Microsoft Visual Studio Tools for the Microsoft Office system (version 3.0)
How to: Loop Through Found Items in Documents

Applies to

The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office.

Project type

  • Document-level projects

  • Application-level projects

Microsoft Office version

  • Word 2003

  • Word 2007

For more information, see Features Available by Application and Project Type.

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

To loop through found items

  1. Declare a Range object.

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

    Visual Basic
    Dim rng As Word.Range = Me.Content
    
    C#
    Word.Range rng = this.Content; 
    

    The following code example can be used in an application-level add-in. This example uses the active document.

    Visual Basic
    Dim rng As Word.Range = Me.Application.ActiveDocument.Content
    
    C#
    Word.Document document = this.Application.ActiveDocument;
    Word.Range rng = document.Content;
    
  2. Use the 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.

    Visual Basic
    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
    
    C#
    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.

    Visual Basic
    MessageBox.Show("Strings found: " & intFound.ToString())
    
    C#
    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

  • 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.

    Visual Basic
    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
    
    C#
    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()); 
    }
    
Application-Level Add-in Example

To loop through items in an application-level add-in

  • The following example shows the complete code for an application-level add-in. To use this code, run it from the ThisAddIn class in your project.

    Visual Basic
    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
    
    C#
    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

Tasks

Concepts

Tags :


Page view tracker