I would like to search and replace text in the header and body of an active
document. However, I'm having trouble figuring out how to code a macro to
search in the header of the document.
I thought that I could perhaps capture the necessary information by going
through the search and replace process using the macro recorder.
Unfortunately, the code that process generated didn't work properly.
Here's the code I am using to search and replace the text in the body of the
active document:
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "-*"
.Replacement.Text = "-"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Some of those parameters could probably be deleted from the code and it
would work fine. But it does take care of the text replacement in the body
of the document.
I don't know if it's possible to do or not, but what I'm actually wanting to
do is search for a bookmark called SaveFileNumber, and if the bookmark has a
value that ends with -*, to delete the * . The bookmark value is used to
provide the name of a file folder that is also created or accessed by the
macro. Since an * isn't a valid character for a filename, the macro needs
to delete the * if it is present.
For example the bookmark SaveFileNumber might contain an initial value of
"2005-TC-000123-*" and the macro needs to change the value to
"2005-TC-000123-".
I didn't know how to go about changing the value of the bookmark, so I opted
to go with the search and replace route. It gets me to the same end result,
especially if I can search the header, but isn't as direct or elegant as I'd
like. Any assistance would be most welcome.
Doug Robbins - Word MVP - 23 Nov 2005 05:07 GMT
See the article "Using a macro to replace text where ever it appears in a
document including Headers, Footers, Textboxes, etc."
http://www.word.mvps.org/FAQs/MacrosVBA/FindReplaceAllWithVBA.htm

Signature
Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.
Doug Robbins - Word MVP
>I would like to search and replace text in the header and body of an active
> document. However, I'm having trouble figuring out how to code a macro to
[quoted text clipped - 49 lines]
> I'd
> like. Any assistance would be most welcome.
Roger Marrs - 23 Nov 2005 18:43 GMT
Thank you Doug. That article helped solve my issue.
Roger
> See the article "Using a macro to replace text where ever it appears in a
> document including Headers, Footers, Textboxes, etc."
[quoted text clipped - 53 lines]
> > I'd
> > like. Any assistance would be most welcome.