How to: Programmatically loop through found items in documents

The Find class has a Microsoft.Office.Interop.Word.Find.Found* property, which returns true whenever a searched-for item is found. You can loop through all instances found in a Range using the Microsoft.Office.Interop.Word.Find.Execute* method.

Applies to: The information in this topic applies to document-level projects and VSTO Add-in projects for Word. For more information, see Features available by Office application and project type.

To loop through found items

  1. Declare a Range object.

    The following code example can be used in a document-level customization.

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

    The following code example can be used in a VSTO Add-in. This example uses the active document.

    Dim rng As Word.Range = Me.Application.ActiveDocument.Content
    
    Word.Document document = this.Application.ActiveDocument;
    Word.Range rng = document.Content;
    
  2. Use the Microsoft.Office.Interop.Word.Find.Found* property in a loop to search for all occurrences of the string in the document, and increment an integer variable by 1 each time the string is found.

    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. Display the number of times the string was found in a message box.

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

    The following examples show the complete method.

Document-level customization example

To loop through items in a document-level customization

  1. The following example shows the complete code for a document-level customization. To use this code, run it from the ThisDocument class in your project.

    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()); 
    }
    

VSTO Add-in example

To loop through items in a VSTO Add-in

  1. The following example shows the complete code for a VSTO Add-in. To use this code, run it from the ThisAddIn class in your project.

    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());
    }
    

See also

How to: Programmatically search for and replace rext in documents
How to: Programmatically set search options in Word
How to: Programmatically define and select ranges in documents
How to: Programmatically restore selections after searches
Optional parameters in Office solutions