Using Find/Replace in Headers

A

Adsoftware

I am using VBA within Word 2002 to try and automatically
replace text in a header. I've tried the storyrange
approach and the looping thru the headers collection but I
CAN'T get the Replace function to work.

When I step through the code, the Find.Found property
returns false, indicating it's not finding my text at
all. HELP.

I'm attaching the generic function I use to do this...

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
'With .StoryRanges(1).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
 
S

siddarth

Its a tested code...will replace text from anything n a doc..be a footer..header...text box..or a paragraph

Sub FindAndReplaceAllStories(ByVal StrFind As String, ByVal StrReplace As String
'++++ Validate Parameters +++
If StrFind = "" The
Exit Su
End I

If StrReplace = "" The
Exit Su
End I
'+++++++++++++++++++++++++++++

Application.ScreenUpdating = Fals

Dim myStoryRange As Rang

'First search the main document using the Selectio
With Selection.Fin

.Text = StrFin
.Replacement.Text = StrReplac
.Forward = Tru
.Wrap = wdFindContinu
.Format = Fals
.MatchCase = Fals
.MatchWholeWord = Fals
.MatchWildcards = Fals
.MatchSoundsLike = Fals
.MatchAllWordForms = Fals
.Execute Replace:=wdReplaceAl

End Wit

'Now search all other stories using Range
For Each myStoryRange In ActiveDocument.StoryRange
If myStoryRange.StoryType <> wdMainTextStory The
With myStoryRange.Fin

.Text = StrFin
.Replacement.Text = StrReplac
.Wrap = wdFindContinu
.Execute Replace:=wdReplaceAl
End Wit
Do While Not (myStoryRange.NextStoryRange Is Nothing
Set myStoryRange = myStoryRange.NextStoryRang
With myStoryRange.Fin

.Text = StrFin
.Replacement.Text = StrReplac
.Wrap = wdFindContinu
.Execute Replace:=wdReplaceAl
End Wit
Loo
End I
Next myStoryRang
End Su
 
S

siddartha pal >

Sub FindAndReplaceAllStories(ByVal StrFind As String, ByVal StrReplace As String
'++++ Validate Parameters +++
If StrFind = "" The
Exit Su
End I

If StrReplace = "" The
Exit Su
End I
'+++++++++++++++++++++++++++++

Application.ScreenUpdating = Fals

Dim myStoryRange As Rang

'First search the main document using the Selectio
With Selection.Fin

.Text = StrFin
.Replacement.Text = StrReplac
.Forward = Tru
.Wrap = wdFindContinu
.Format = Fals
.MatchCase = Fals
.MatchWholeWord = Fals
.MatchWildcards = Fals
.MatchSoundsLike = Fals
.MatchAllWordForms = Fals
.Execute Replace:=wdReplaceAl

End Wit

'Now search all other stories using Range
For Each myStoryRange In ActiveDocument.StoryRange
If myStoryRange.StoryType <> wdMainTextStory The
With myStoryRange.Fin

.Text = StrFin
.Replacement.Text = StrReplac
.Wrap = wdFindContinu
.Execute Replace:=wdReplaceAl
End Wit
Do While Not (myStoryRange.NextStoryRange Is Nothing
Set myStoryRange = myStoryRange.NextStoryRang
With myStoryRange.Fin

.Text = StrFin
.Replacement.Text = StrReplac
.Wrap = wdFindContinu
.Execute Replace:=wdReplaceAl
End Wit
Loo
End I
Next myStoryRang
End Su
 
S

siddartha

there are 11 types of story ranges in a doc. So that code will cater for text in form be in a text box be it in a paragrap

pls revert back for feedback..

thks n regard
siddartha
 
P

Peter Hewett

Hi siddartha

Actually there are more! Up to and including Word XP there are 11, in Word 2003 there are
17 story types.

Also you can tighten the code to iterate the story ranges to:

Public Sub SREntireDoc()
Dim rngStory As Word.Range
Dim lngJunk As Long

' Word missing first Header/Footer bug workaround
lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType

' Iterate through all story types in the current document
For Each rngStory In ActiveDocument.StoryRanges

' Iterate through all linked stories
SearchAndReplaceInStory rngStory, "page", "dog"
Next
End Sub

Public Sub SearchAndReplaceInStory(ByVal rngStory As Word.Range, _
ByVal strSearch As String, _
ByVal strReplace As String)
Do Until (rngStory Is Nothing)
With rngStory.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = strSearch
.Replacement.Text = strReplace
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False

.Execute Replace:=wdReplaceAll
End With
Set rngStory = rngStory.NextStoryRange
Loop
End Sub

HTH + Cheers - Peter

This version also takes care of a bug that causes problems with some header/footers
(depends on document structure).

HTH + Cheers - Peter
 

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