【问题标题】:Search and replace in MSWord header and footer using delphi xe3使用delphi xe3在MSWord页眉和页脚中搜索和替换
【发布时间】: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 ...

标签: delphi ms-word


【解决方案1】:

它不起作用,因为您正在设置 Selection 的 Find 对象,然后使用 header 的 Range 的 Find 对象。这些是不同的东西。

如果你修改这些行

WrdApp.ActiveDocument.Sections.Item(1).Headers.Item(wdHeaderFooterPrimary).Range.Find.Execute(Replace := wdReplaceAll);

如下所示(您需要正确使用 Delphi 语法)

WrdApp.ActiveDocument.Sections.Item(1).Headers.Item(wdHeaderFooterPrimary).Range.Select;
WrdApp.Selection.Find.Execute(Replace := wdReplaceAll);

您应该会看到改进,但我的猜测是(a)如果您可以避免使用 Selection 对象,这是更可取的,并且(b)如果您需要处理具有不同页眉和页脚的更一般情况,事情可以得到更复杂一点。所以我建议你去

"Using a macro to replace text where ever it appears in a document" 在 Word MVP 网站上研究他们拥有的代码。从 VBA->Delphi 翻译应该很容易。

【讨论】:

    【解决方案2】:

    我找到了答案,我只需要添加这一行来将焦点设置到标题:

    WrdApp.ActiveWindow.ActivePane.View.SeekView := wdSeekCurrentPageHeader;
    

    然后再次运行搜索命令。

    谢谢你们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-08
      • 2019-07-04
      • 1970-01-01
      • 2013-11-18
      • 2011-03-10
      • 2018-02-02
      • 1970-01-01
      • 2011-06-26
      相关资源
      最近更新 更多