I want to find & replace a text in word document. I created a macro as bellow.
Sub Macro1()
ActiveDocument.Content.Find.Execute FindText:="#Text1", ReplaceWith:="acca", _
Replace:=wdReplaceAll
End Sub
It replaced all occurred but not in header/footer!! How forced to work on entire document include header/body/footer?
I've always used this VBA code to Find/Replace, and it will do Headers/Footers along with the body of the document:
Dim myStoryRange As Range
For Each myStoryRange In ActiveDocument.StoryRanges
With myStoryRange.Find
.Text = "Text to find to replace goes here"
.Replacement.Text = "And the replacement text goes here"
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
Do While Not (myStoryRange.NextStoryRange Is Nothing)
Set myStoryRange = myStoryRange.NextStoryRange
With myStoryRange.Find
.Text = "Text to find to replace goes here"
.Replacement.Text = "And the replacement text goes here"
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
Loop
Next myStoryRange
You can also copy and paste it a bunch of times in the same Sub to replace different strings at the same time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With