replace text in all header stories of a document with severalsections

A

andreas

Dear Experrts,
for some specific reason, I would like to be able to replace text in
ALL HEADER stories running just one macro.
If I set the range, I am only able to set the PRIMARYHEADER OR
EVENPAGES HEADER OR THE FIRSTPAGE HEADER STORY, but not all at the
same time.

How do I have to alter below macro to include all HEADER STORIES in
all sections (The documents I am working on have several sections)?

Help is appreciated. Thank you very much in advance for your valuable
help.




Sub ReplaceTextHeaderStories()
Dim rng As Range
Dim AskText As String
Dim AskReplacementText As String


AskText = InputBox("Please enter the text that needs to be replaced?",
"Text to be replaced")
AskReplacementText = InputBox("Please enter the replacement text",
"Replacement Text")

Set rng = ActiveDocument.StoryRanges(wdEvenPagesHeaderStory)
With rng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = AskText
.Replacement.Text = AskReplacementText
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchWildcards = False

.Execute Replace:=wdReplaceAll
End With

End Sub
 
F

fumei via OfficeKB.com

I would agree with Jay that is best to carefully read the entire article, and
that there may be some other things to consider.

But to amswer your question directly, it is best to use objects. Since you
want to do something with all headers, and all Sections, then use a Header
object for the headers, and a Section object for the sections.

Sub ReplaceTextHeaderStories()
Dim oHF As HeaderFooter
Dim oSection As Section
Dim AskText As String
Dim AskReplacementText As String

AskText = InputBox("Please enter the text that needs to be replaced?",
"Text to be replaced")
AskReplacementText = InputBox("Please enter the replacement text",
"Replacement Text")

For Each oSection In ActiveDocument
For Each oHF In oSection.Headers
oHF.Range.Text = _
Replace(oHF.Range.Text, AskText, AskReplacementText)
Next
Next
End Sub

The logic:

FOR EACH Section, and FOR EACH header in that Section,
Replace AskText with AskReplacementText

Done.

But again, I would encourage you to take jay's advice and read the reference
article fully.
 
A

andreas

I would agree with Jay that is best to carefully read the entire article, and
that there may be  some other things to consider.

But to amswer your question directly, it is best to use objects.  Since you
want to do something with all headers, and all Sections, then use a Header
object for the headers, and a Section object for the sections.

Sub ReplaceTextHeaderStories()
Dim oHF As HeaderFooter
Dim oSection As Section
Dim AskText As String
Dim AskReplacementText As String

AskText = InputBox("Please enter the text that needs to be replaced?",
"Text to be replaced")
AskReplacementText = InputBox("Please enter the replacement text",
"Replacement Text")

For Each oSection In ActiveDocument
   For Each oHF In oSection.Headers
        oHF.Range.Text = _
             Replace(oHF.Range.Text, AskText, AskReplacementText)
   Next
Next
End Sub

The logic:

FOR EACH Section, and FOR EACH header in that Section,
   Replace AskText with AskReplacementText

Done.

But again, I would encourage you to take jay's advice and read the reference
article fully.

Hey,

thank you for your valuable adivce. It is working as desired.
Regards, Andreas
 
A

andreas

Seehttp://www.word.mvps.org/FAQs/Customization/ReplaceAnywhere.htm. There
are more considerations than just reaching all the headers, so read all the
way to the end.

--
Regards,
Jay Freedman
Microsoft Word MVP        FAQ:http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.











- Zitierten Text anzeigen -

Hey Jay,
thanks for the good advice. I already had a look at it and will also
be able to use it.
Regards, Andreas
 

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