VBA Find Text in Word Doc in Excel doesn't work - stuck
This is half the build process, sorry if confused
I have this code where I define a dictionary in excel. From there, I want to find text from "keywords" in the Word document, and once found, I want to move on to other encodings.
The problem is, I only get to this .find
part, I can't work on it for the rest of my life, why it can't find anything.
Attention is drawn to the following:
For Each Key In Dict
After that, all I'm asking is to find the text in the string C
. I know the fact that C
contains a value because I added a MsgBox
check and added it to the clipboard as well, so I can try to find the text manually - if I can search manually, so can
However, when running/stepping through the code, the command .find.execute
seems to be ignored as if it's not even trying to search for the Document, and blnFound
every time Boolean returns False, jumps to Next
. At that time my screen also showed the document (opened by the code) but nothing happened.
Can someone tell me what I am doing wrong here? I am totally confused.
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. I also tried
Wrd.Selection.Find.text = C
blnFound = Wrd.Selection.Find.Execute
and add it before the lookup
TokenDoc.Activate
Is this what you want to try ( tested on local template files )
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