使用delphi xe3在MSWord页眉和页脚中搜索和替换
问题描述:
我想在单词的页眉和页脚中搜索特定的单词,然后将其替换为数据库中的单词.
I would like to search in a word header and footer for specific words, and then replace them with words from my database.
当前,我可以在word文档中的任何位置搜索和替换单词,但页眉和页脚除外.
Currently i can search and replace words anywhere in the word document except for the header and footer.
有人可以帮我吗?
正常搜索的代码(有效):
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);
答
我找到了答案,我只需要添加此行即可将焦点设置到标题:
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.
谢谢你们.