How to: Loop Through Found Items in Documents
Some code examples in this topic use the this or Me keyword or the
Theobject has a property, which returns True whenever a searched-for item is found. You can loop through all instances found in a using the method. You can make use of this in your code, as shown in the following example.
To loop through found items
Declare a Range object.
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.
Display the number of times the string was found in a MessageBox.
The following example shows the complete method.
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