I'm having some trouble in writing a routine for Excel to replace text in a word document. I have a list of the replacement text strings, and the dummy text strings they're going to replace contained in the excel document. I have a word document with the dummy text in the correct places. I'm trying to loop through this list, contained in excel, replacing everything in the word document.
It works fine replacing text in the body - written as below, it gets everything in the document outside the header with no problems.
I found that it doesn't replace text in the header - which is a common problem, and I've found direction to fix this in a routine internet search. However, all the solutions I've found don't seem to be working with the format above - or, at least, I can't make it work because I'm doing something wrong.
Can anyone point me in the right direction? Appreciate any help I can get - thanks.
It works fine replacing text in the body - written as below, it gets everything in the document outside the header with no problems.
Code:
Dim Filename As StringDim wordapp As Word.Application
Dim wdDoc As Word.Document
Dim lastrow As Long
Dim replace As Worksheet
Set replace = Worksheets("replacementlist")
Set wordapp = CreateObject("word.Application")
Filename = ThisWorkbook.Path & "\TEXT REPLACE TEST.docm"
Set wdDoc = wordapp.Documents.Open(Filename)
wordapp.Visible = True
lastrow = replace.Range("a1000000").End(xlUp).Row
For n = 1 To lastrow
DummyText = replace.Range("b" & n).Value
ReplaceText = replace.Range("c" & n).Value
With MyRange.Find
.Text = DummyText
.replacement.Text = ReplaceText
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
MyRange.Find.Execute replace:=wdReplaceAll
Next n
I found that it doesn't replace text in the header - which is a common problem, and I've found direction to fix this in a routine internet search. However, all the solutions I've found don't seem to be working with the format above - or, at least, I can't make it work because I'm doing something wrong.
Can anyone point me in the right direction? Appreciate any help I can get - thanks.