【发布时间】:2014-05-12 19:49:29
【问题描述】:
我想在单词页眉和页脚中搜索特定单词,然后用我的数据库中的单词替换它们。
目前我可以在除了页眉和页脚之外的任何地方搜索和替换单词。
谁能帮我解决这个问题?
正常搜索的代码(有效):
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;
页眉和页脚搜索代码(不起作用):
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);
【问题讨论】:
-
自从我使用 Delphi 的 MS Word 自动化以来已经有一段时间了,但是 ISTR 认为 Word 有一个“故事”的概念(大致意思是一种文本流(正文、页眉、页脚、等) - 请参阅提供的 Word 自动化 PAS 文件中的 WdStoryType),我隐约记得(可能是错误的)可以使用它来指定像查找这样的操作适用于文本的哪些部分。 Fwiw ...