[英]VBA Find Text in Word Doc from Excel Not Working - Stumped
This is something half way through being built so sorry if it's confusing 这是构建过程的一半,如果感到困惑,请抱歉
I have this code where I've defined a dictionary in excel. 我在excel中定义字典的地方有此代码。 From there I want to find text from the 'Key' in a Word Document then once it's found I want to carry on with other coding.
从那里我想从Word文档中的“关键字”中查找文本,然后一旦找到,我就希望继续进行其他编码。
The problem is, I've only gotten as far as the .find
part and I can't work out for the life of me why it's not finding anything. 问题是,我只找到了
.find
部分,而我无法终生解决为什么找不到任何东西。
Draw your attention to the line: 提请注意以下内容:
For Each Key In Dict
All I've asked after that is to find the text in string C
. 我所要问的就是在字符串
C
找到文本。 I know for a fact that C
contains a value, since I've added a MsgBox
to check and I've also added it to the Clipboard so I can try and manually find the text - and I can if I search manually 我知道
C
包含一个值,因为我已经添加了一个MsgBox
进行检查,并且我也将其添加到了剪贴板中,所以我可以尝试手动查找文本-如果我手动搜索也可以
But upon running/stepping through the code the .find.execute
command seems to be somewhat ignored as though it's not even trying to search through the Document and blnFound
Boolean comes back False every time, jumping to Next
. 但是在运行/单步执行代码时,似乎似乎忽略了
.find.execute
命令,好像它甚至没有尝试搜索Document一样,并且blnFound
布尔每次都返回False,跳转到Next
。 I also have the document (Opened by the code) displaying on my screen at the time and nothing happens on it. 当时我的屏幕上还显示有文档(由代码打开),但没有任何反应。
Can someone advise me of what I'm doing wrong here? 有人可以告诉我我在这里做错了什么吗? I'm completely baffled.
我完全困惑。
Thanks! 谢谢!
Sub FindReplaceInWord2()
Dim Wbk As Workbook: Set Wbk = ThisWorkbook
Dim Wrd As New Word.Application
Dim Dict As Object
Dim RefList As Range, RefElem As Range
Dim A As String
Dim B As String
Dim C As String
Dim test As New DataObject
Dim blnFound As Boolean
Wrd.Visible = True
Dim TokenDoc As Document
Set TokenDoc = Wrd.Documents.Open("\\SERVER\Client\Table.dot")
Set Dict = CreateObject("Scripting.Dictionary")
Set RefList = Wbk.Sheets("Sheet1").Range("A1:A236")
With Dict
For Each RefElem In RefList
On Error Resume Next
If Not .Exists(RefElem) And Not IsEmpty(RefElem) Then
A = RefElem.Value
.Add RefElem.Value, RefElem.Offset(0, 1).Value
B = RefElem.Value
End If
Next RefElem
End With
For Each Key In Dict
Set test = New DataObject
'MsgBox Key
test.SetText (Key)
test.PutInClipboard
C = Key
MsgBox C
With Wrd.ActiveDocument.Find
.Text = C
End With
blnFound = Wrd.ActiveDocument.Find.Execute
If blnFound = True Then
MsgBox = "Yay for working it out"
Else
MsgBox = "Boo, it didn't Work"
End If
Next Key
End Sub
PS. PS。 I've also tried
我也尝试过
Wrd.Selection.Find.text = C
blnFound = Wrd.Selection.Find.Execute
and adding this before the find 并将其添加到查找之前
TokenDoc.Activate
Is this what you are trying ( Tried and Tested on a Local Template File ) 这是您要尝试的吗( 在本地模板文件上经过测试 )
Sub FindReplaceInWord2()
Dim Wbk As Workbook: Set Wbk = ThisWorkbook
Dim RefList As Range, RefElem As Range
Dim col As New Collection
Dim itm
Dim blnFound As Boolean
Dim Wrd As New Word.Application
Dim TokenDoc As Document
Wrd.Visible = True
'Set TokenDoc = Wrd.Documents.Open("D:\Users\SidzPc\Desktop\Temp\Table.dot")
Set TokenDoc = Wrd.Documents.Open("\\SERVER\Client\Table.dot")
Set RefList = Wbk.Sheets("Sheet1").Range("A1:A236")
For Each RefElem In RefList
On Error Resume Next
col.Add RefElem.Value, CStr(RefElem.Value)
On Error GoTo 0
Next RefElem
For Each itm In col
With Wrd.Selection.Find
.Text = itm
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
blnFound = Wrd.Selection.Find.Execute
If blnFound = True Then
MsgBox "Yay for working it out"
Else
MsgBox "Boo, it didn't Work"
End If
Next itm
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.