如何:依次通过在文档中找到的项

更新:2007 年 11 月

适用对象

本主题中的信息仅适用于指定的 Visual Studio Tools for Office 项目和 Microsoft Office 版本。

项目类型

  • 文档级项目

  • 应用程序级项目

Microsoft Office 版本

  • Word 2003

  • Word 2007

有关更多信息,请参见按应用程序和项目类型提供的功能

Find 类具有一个 Found 属性,只要找到搜索的项,此属性就会返回 true。可以使用 Execute 方法依次通过在 Range 中找到的所有实例。

依次通过找到的项

  1. 声明一个 Range 对象。

    下面的代码示例可用于文档级自定义项。

    Dim rng As Word.Range = Me.Content
    
    Word.Range rng = this.Content; 
    

    下面的代码示例可用于应用程序级外接程序。此示例使用活动文档。

    Dim rng As Word.Range = Me.Application.ActiveDocument.Content
    
    Word.Document document = this.Application.ActiveDocument;
    Word.Range rng = document.Content;
    
  2. 使用循环中的 Found 属性在文档中搜索该字符串的所有匹配项,每次找到该字符串,都将整数变量的值增加 1。

    rng.Find.ClearFormatting()
    rng.Find.Forward = True
    rng.Find.Text = "find me"
    
    rng.Find.Execute()
    
    Do While rng.Find.Found = True
        intFound += 1
        rng.Find.Execute()
    Loop
    
    rng.Find.ClearFormatting(); 
    rng.Find.Forward = true; 
    rng.Find.Text = "find me"; 
    
    rng.Find.Execute(
        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, ref missing);
    
    while (rng.Find.Found) 
    { 
        intFound++;
        rng.Find.Execute(
            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, ref missing);
    } 
    
  3. 在消息框中显示找到该字符串的次数。

    MessageBox.Show("Strings found: " & intFound.ToString())
    
    MessageBox.Show("Strings found: " + intFound.ToString()); 
    

下面的示例演示完整的方法。

文档级自定义项示例

遍历文档级自定义项中的项

  • 下面的示例显示了文档级自定义项的完整代码。若要使用此代码,请从项目内的 ThisDocument 类中运行此代码。

    Private Sub FindLoop()
        Dim intFound As Integer = 0
        Dim rng As Word.Range = Me.Content
    
        rng.Find.ClearFormatting()
        rng.Find.Forward = True
        rng.Find.Text = "find me"
    
        rng.Find.Execute()
    
        Do While rng.Find.Found = True
            intFound += 1
            rng.Find.Execute()
        Loop
    
        MessageBox.Show("Strings found: " & intFound.ToString())
    End Sub
    
    private void FindLoop() 
    { 
        int intFound = 0; 
        Word.Range rng = this.Content; 
    
        rng.Find.ClearFormatting(); 
        rng.Find.Forward = true; 
        rng.Find.Text = "find me"; 
    
        rng.Find.Execute(
            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, ref missing);
    
        while (rng.Find.Found) 
        { 
            intFound++;
            rng.Find.Execute(
                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, ref missing);
        } 
    
        MessageBox.Show("Strings found: " + intFound.ToString()); 
    }
    

应用程序级外接程序示例

遍历应用程序级外接程序中的项

  • 下面的示例显示了应用程序级外接程序的完整代码。若要使用此代码,请从项目内的 ThisAddIn 类中运行此代码。

    Private Sub FindLoop()
        Dim intFound As Integer = 0
        Dim rng As Word.Range = Me.Application.ActiveDocument.Content
    
        rng.Find.ClearFormatting()
        rng.Find.Forward = True
        rng.Find.Text = "find me"
    
        rng.Find.Execute()
    
        Do While rng.Find.Found = True
            intFound += 1
            rng.Find.Execute()
        Loop
    
        MessageBox.Show("Strings found: " & intFound.ToString())
    End Sub
    
    private void FindLoop()
    {
        int intFound = 0;
        Word.Document document = this.Application.ActiveDocument;
        Word.Range rng = document.Content;
    
        rng.Find.ClearFormatting();
        rng.Find.Forward = true;
        rng.Find.Text = "find me";
    
        rng.Find.Execute(
            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, ref missing);
    
        while (rng.Find.Found)
        {
            intFound++;
            rng.Find.Execute(
                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, ref missing);
        }
    
        MessageBox.Show("Strings found: " + intFound.ToString());
    }
    

请参见

任务

如何:在文档中搜索文本

如何:在 Word 中设置搜索选项

如何:在文档中定义和选择范围

如何:在搜索后还原选定内容

概念

了解 Office 解决方案中的可选参数