使用delphi xe3在MSWord页眉和页脚中搜索和替换 [英] Search and replace in MSWord header and footer using delphi xe3
本文介绍了使用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.
谢谢你们.
这篇关于使用delphi xe3在MSWord页眉和页脚中搜索和替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文