Microsoft KB Archive/276475

From BetaArchive Wiki
Knowledge Base


WD97: How to Perform a Word Count of a Specific Word in a Document

Article ID: 276475

Article Last Modified on 10/11/2006



APPLIES TO

  • Microsoft Word 95 Standard Edition



This article was previously published under Q276475

SUMMARY

This article demonstrates how to use a Word Visual Basic for Applications macro to perform a word count of a specific word in a document.

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
For more information about how to use the sample code in this article, click the article number below to view the article in the Microsoft Knowledge Base:

173707 OFF97: How to Run Sample Code from Knowledge Base Articles


Sub Find()

   Dim MyCount As Integer
   Dim MyWord As String
   Dim Total As Integer
   Dim intI As Integer

   'Prompt the user for the word to search for.
   MyWord = InputBox("What word are you looking for?:")

   'Select the entire document
   Selection.WholeStory

   'Count the total number of words in the document
   Total = Application.Selection.Range.Words.Count

   'Enumerate through each word selected, removing leading
   ' and trailing spaces
   For intI = 1 To Total

      With Selection.Words(intI)

         If Trim(.Text) = MyWord Then
            MyCount = MyCount + 1
         End If

      End With

   Next intI

   'Display results
   MsgBox "Found " & MyCount & " occurrences of: " & MyWord & _
      " out of a total of: " & Total & " words"

End Sub
                


Additional query words: vba word count selection document story

Keywords: kbdtacode kbhowto KB276475