Replacing Text in Word .doc Header from Excel VBA Macro

corollary

New Member
Joined
May 31, 2014
Messages
18
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.

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.
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)

Forum statistics

Threads
1,215,268
Messages
6,123,966
Members
449,137
Latest member
yeti1016

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top