以编程方式搜索和替换文档中的文本
Find 对象是 Selection 和 Range 对象的成员,可使用其中任何一个来搜索 Microsoft Office Word 文档中的文本。 替换命令是查找命令的扩展。
使用 Find 对象循环访问 Microsoft Office Word 文档并搜索特定文本、格式或样式,使用 Replacement 属性替换任何找到的项。
适用于: 本主题中的信息适用于 Word 的文档级项目和 VSTO 外接程序项目。 有关详细信息,请参阅办公室应用程序和项目类型提供的功能。
使用 Selection 对象
使用 Selection 对象查找文本时,指定的任何搜索条件均只能应用于当前选定的文本。 如果 Selection 是插入点,则搜索文档。 找到符合搜索条件的项时,将自动将其选中。
请务必注意,Find 条件是累积性的,这意味着会将条件添加到之前的搜索条件。 搜索前使用 ClearFormatting 方法清除之前的搜索的格式。
使用 Selection 对象查找文本
将搜索字符串分配给变量。
清除之前的搜索的格式。
执行搜索,并显示消息框和结果。
if (Application.Selection.Find.Execute(ref findText, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing)) { MessageBox.Show("Text found."); } else { MessageBox.Show("The text could not be located."); }
以下示例显示完整的方法。
private void SelectionFind() { object findText = "find me"; Application.Selection.Find.ClearFormatting(); if (Application.Selection.Find.Execute(ref findText, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing)) { MessageBox.Show("Text found."); } else { MessageBox.Show("The text could not be located."); } }
使用 Range 对象
使用 Range 对象使你能够搜索文本,而无需在用户界面中显示任何内容。 如果找到与搜索条件匹配的文本,则对象Find返回 True;如果找不到,则返回 False。 如果找到了文本,则它还重新定义 Range 对象以匹配搜索条件。
使用 Range 对象查找文本
定义 Range 对象,该对象包含文档中的第二个段落。
下面的代码示例可用于文档级自定义项。
以下代码示例可用于 VSTO 外接程序。 本示例使用活动文档。
在消息框中显示搜索结果,然后选择 Range 以使其可见。
如果搜索失败,则选中第二段;如果成功,则显示搜索条件。
下面的示例显示文档级自定项的完整代码。 若要使用此示例,请从项目的
ThisDocument
类中运行代码。private void RangeFind() { object findText = "find me"; Word.Range rng = this.Paragraphs[2].Range; rng.Find.ClearFormatting(); if (rng.Find.Execute(ref findText, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing)) { MessageBox.Show("Text found."); } else { MessageBox.Show("Text not found."); } rng.Select(); }
下面的示例显示 VSTO 外接程序的完整代码。 若要使用此示例,请从项目的 ThisAddIn
类中运行代码。
private void RangeFind()
{
object findText = "find me";
Word.Document document = this.Application.ActiveDocument;
Word.Range rng = document.Paragraphs[2].Range;
rng.Find.ClearFormatting();
if (rng.Find.Execute(ref findText,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing))
{
MessageBox.Show("Text found.");
}
else
{
MessageBox.Show("Text not found.");
}
rng.Select();
}
在文档中搜索和替换文本
以下代码将搜索当前所选内容,并将字符串的所有匹配项替换为“找到”字符串。
在文档中搜索并替换文本
将以下示例代码添加到项目的
ThisDocument
或ThisAddIn
类中。private void SearchReplace() { Word.Find findObject = Application.Selection.Find; findObject.ClearFormatting(); findObject.Text = "find me"; findObject.Replacement.ClearFormatting(); findObject.Replacement.Text = "Found"; object replaceAll = Word.WdReplace.wdReplaceAll; findObject.Execute(ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref replaceAll, ref missing, ref missing, ref missing, ref missing); }
Find 类具有 ClearFormatting 方法,而 Replacement 类也具有自己的 ClearFormatting 方法。 执行查找和替换操作时,必须使用这两个对象的 ClearFormatting 方法。 如果仅在 Find 对象上使用该方法,则可能会在替换文本中得到意外的结果。
使用 Find 对象的 Execute 方法来替换每个找到的项。 若要指定要替换的项,请使用 Replace 参数。 此参数可能是以下 WdReplace 值之一:
wdReplaceAll 将替换所有找到的项。
wdReplaceNone 不会替换任何找到的项。
wdReplaceOne 将替换找到的第一个项。