Microsoft KB Archive/86259

{|
 * width="100%"|

WD: Sample 'While EditFindFound' Looping Macro

 * }

Q86259

-

The information in this article applies to:


 * Microsoft Word for Windows, versions 1.0, 1.1, 1.1a, 2.0, 2.0a, 2.0a-CD, 2.0b, 2.0c, 6.0, 6.0a, 6.0c
 * Microsoft Word for Windows, versions 7.0, 7.0a
 * Microsoft Word for the Macintosh, versions 6.0, 6.0.1

-

SUMMARY
The following sample macros can be used to search for the occurrence of text in a Microsoft Word document.

MORE INFORMATION
NOTE: Microsoft provides macros &quot;as is&quot; without warranty of any kind, either expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

Word 6.x, 7.x
This sample macro prompts you for a text string to search for and then returns the number of times the text was found in a message box. The macro starts at the top of the document and counts the total number of occurrences in the document.

  Sub MAIN StartOfDocument counter = 0 On Error Goto bye searchtext$ = InputBox$(&quot;Enter text to search for?&quot;) EditFind .Find = searchtext$, .Direction = 0 If Not EditFindFound Then MsgBox &quot;Search Text Not Found&quot; Goto bye End If     While EditFindFound counter = counter + 1 EditFind .Find = searchtext$, .Direction = 0 Wend MsgBox searchtext$ + &quot; was found&quot; + Str$(counter) + &quot;time(s)&quot; bye: End Sub

Word 2.x
  Sub MAIN StartOfDocument counter = 0 On Error Goto bye searchtext$ = InputBox$(&quot;Enter text to search for?&quot;) EditFind .Find = searchtext$, .Direction = 2 If Not EditFindFound Then MsgBox &quot;Search Text Not Found&quot; Goto bye End If     While EditFindFound counter = counter + 1 EditFind .Find = searchtext$, .Direction = 2 Wend MsgBox searchtext$ + &quot; was found&quot; + Str$(counter) + &quot;time(s)&quot; bye: End Sub

Word 1.x
Make the following substitutions in the Word 2.x macro:


 * Change the EditFind commands to the following syntax:
 * Change the line, &quot;If Not EditFindFound Then&quot; to the following:
 * Change the line, &quot;While EditFindFound&quot; to the following:

The counter variable is used to count the number of times the search text is found (counter = counter + 1). If the search text is not found in the document, a message box informs you that the search text was not found.

If you want to apply a format or perform commands when the search text is found, add the appropriate commands below the EditFind command. For example, in Word 2.x:

  EditFind .Find = searchtext$, .Direction = 2 FormatCharacter .Bold = 1    'Makes search text bold

While EditFindFound counter = counter + 1 EditFind .Find = searchtext$, .Direction = 2 FormatCharacter .Bold = 1    'Makes search text bold Wend If the search text is found, the text formatting is changed to bold and the counter increments by 1. Other WordBasic commands can be used in place of the line that reads &quot;FormatCharacter .Bold = 1.&quot; (WordBasic commands entered below the EditFind command are executed when the search text is found.)