Reputation: 51
I would like to search in a word header and footer for specific words, and then replace them with words from my database.
Currently i can search and replace words anywhere in the word document except for the header and footer.
Can anybody help me with this?
Code for normal search(that works):
Procedure FindAndReplace(Find,Replace:String);
Begin
//Initialize parameters
WrdApp.Selection.Find.ClearFormatting;
WrdApp.Selection.Find.Text := Find;
WrdApp.Selection.Find.Replacement.Text := Replace;
WrdApp.Selection.Find.Forward := True;
WrdApp.Selection.Find.Wrap := wdFindContinue;
WrdApp.Selection.Find.Format := False;
WrdApp.Selection.Find.MatchCase := False;
WrdApp.Selection.Find.MatchWholeWord := wrfMatchCase in Flags;
WrdApp.Selection.Find.MatchWildcards :=wrfMatchWildcards in Flags;
WrdApp.Selection.Find.MatchSoundsLike := False;
WrdApp.Selection.Find.MatchAllWordForms := False;
{ Perform the search}
if wrfReplaceAll in Flags then
WrdApp.Selection.Find.Execute(Replace := wdReplaceAll)
else
WrdApp.Selection.Find.Execute(Replace := wdReplaceOne);
End;
Code for header and footer search(doesnt work):
WrdApp.Selection.Find.ClearFormatting;
WrdApp.Selection.Find.Text := 'Class';
WrdApp.Selection.Find.Replacement.Text := grade;
WrdApp.Selection.Find.Forward := True;
WrdApp.Selection.Find.Wrap := wdFindContinue;
WrdApp.Selection.Find.Format := False;
WrdApp.Selection.Find.MatchCase := False;
WrdApp.Selection.Find.MatchWholeWord := wrfMatchCase in Flags;
WrdApp.Selection.Find.MatchWildcards :=wrfMatchWildcards in Flags;
WrdApp.Selection.Find.MatchSoundsLike := False;
WrdApp.Selection.Find.MatchAllWordForms := False;
{ Perform the search}
if wrfReplaceAll in Flags then
WrdApp.ActiveDocument.Sections.Item(1).Headers.Item(wdHeaderFooterPrimary).Range.Find.Execute(Replace := wdReplaceAll)
else
WrdApp.ActiveDocument.Sections.Item(1).Headers.Item(wdHeaderFooterPrimary).Range.Find.Execute(Replace := wdReplaceOne);
Upvotes: 0
Views: 3903
Reputation: 51
I found the answer, i just had to add this line to set the focus to the header:
WrdApp.ActiveWindow.ActivePane.View.SeekView := wdSeekCurrentPageHeader;
and then run the search command again.
Thank you guys.
Upvotes: 1
Reputation:
It won't work because you are setting up the Find object of the Selection, then using the Find object of the Range of the header. These are different things.
If you modify these lines
WrdApp.ActiveDocument.Sections.Item(1).Headers.Item(wdHeaderFooterPrimary).Range.Find.Execute(Replace := wdReplaceAll);
to be something like the following (you'll need to get the Delphi syntax right)
WrdApp.ActiveDocument.Sections.Item(1).Headers.Item(wdHeaderFooterPrimary).Range.Select;
WrdApp.Selection.Find.Execute(Replace := wdReplaceAll);
You should see an improvement, but my guess (a) it's prefereable if you can avoid using the Selection object, and (b) if you need to deal with a more general situation with different headers and footers, things can get a bit more complicated. So I would suggest that you go to
"Using a macro to replace text where ever it appears in a document" on the Word MVPs website and study the code they have. Translation from VBA->Delphi should be quite easy.
Upvotes: 1