Search for text in the header? Or in a bookmark?

R

Roger Marrs

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.
 

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