Find/Replace Text in Headers

A

adsoftware

The find/replace feature of VBA doesn't seem to work
within headers & footers. HELP.

I'm attaching my generic code below for your review.

Sub ReplaceHeadersFooters(sFind As String, sReplace As
String)

Dim MySelection As Variant
Dim vntStories As Variant
Dim x As Long

With ActiveDocument


For x = 0 To .StoryRanges.Count - 1
Set MySelection = ActiveDocument.StoryRanges(1)

With MySelection.Find
.Text = sFind
.Replacement.Text = sReplace
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With

Next x
End With


End Sub
 
H

Helmut Weber

Hi,
For x = 0 To .StoryRanges.Count - 1
Set MySelection = ActiveDocument.StoryRanges(1)
you are always accessing "ActiveDocument.StoryRanges(1)"
means, the very same StoryRange, which is StoryRange number 1.
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top