Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2005
Visual Studio
Word Tasks
 How to: Loop Through Found Items in...

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
Visual Studio Tools for the Microsoft Office System
How to: Loop Through Found Items in Documents

NoteNote

Some code examples in this topic use the this or Me keyword or the Globals class in a way that is specific to document-level customizations, or they rely on features of document-level customizations such as host controls. These examples can be compiled only if you have the required applications installed. For more information, see Features Available by Product Combination.

The Find object 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. You can make use of this in your code, as shown in the following example.

To loop through found items

  1. Declare a Range object.

    Visual Basic
    Dim rng As Word.Range = Me.Content
    
    C#
    Word.Range rng = this.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 MessageBox.

    Visual Basic
    MessageBox.Show("Strings found: " & intFound.ToString())
    
    C#
    MessageBox.Show("Strings found: " + intFound.ToString()); 
    

The following example shows the complete method.

Example

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

See Also

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker