Home Listing method 3 Ways to Count the Number of Occurrences of a Specific Word or Phrase in a Word Document

3 Ways to Count the Number of Occurrences of a Specific Word or Phrase in a Word Document

0
Image: prima91/Adobe Stock

Returning the number of times a word or phrase appears in a Microsoft Word document is a common task for many writers and editors. You don’t want to overuse words. On the other hand, you might want to make sure that you type in a customer’s name frequently when working with marketing materials. Fortunately, Word has two built-in methods for counting occurrences of a specific word. If that’s not flexible enough, you can use a Word VBA sub-procedure. In this article, I will show you both built-in methods and a procedure.

TO SEE: Software Installation Policy (TechRepublic Premium)

I’m using Microsoft 365 on a 64-bit Windows 10 system, but you can use an older version. I recommend that you hold off on upgrading to Windows 11 until all issues are resolved. For your convenience, you can download the .docm, .doc and .cls demo files. The demo files are two sets of the same text generated by Word’s RAND() function, but you’ll want to download the demo files for the Word VBA procedure. Word for the web does not support VBA, but it does support a built-in method.

About Word’s search feature

If an on-the-fly answer is enough, Word’s Find feature is the way to go. For example, let’s search the demo file for the word “video” as follows:

  1. On the Home tab, click Edit and choose Search from the drop-down list.
  2. In the resulting pane, enter video.

That’s it! As you can see in Figure Athis feature displays the number of times the word video appears in the current document.

Figure A

Word displays the number in the Navigation Pane.
Word displays the number in the Navigation Pane.

Additionally, you can click on the links below to access each occurrence. As long as the Find function is active, the yellow highlight remains. When you close the navigation pane, the highlights disappear.

As simple as it sounds, there is a second way. Press Ctrl + H or choose Replace from the Edit drop-down list and click the Find tab. Enter video in the Search control, click Reading Highlights, then select Highlight All. Figure B shows similar results. The big difference is that the highlight remains when you close the Find and Replace dialog box. It’s handy for navigation, but as soon as you try to work in the document, the highlights disappear.

Figure B

This will display the words you are looking for in Word by highlighting them.
This will display the words you are looking for in Word by highlighting them.

Both methods are quick and easy and work in Word for the web. If either meets your needs, you don’t need to work any harder. The only feature missing in Word for the web is highlighting; it does not remain in the document after you close the Find and Replace dialog box. If you need something a little more flexible, you might need a Word VBA procedure.

A VBA procedure for finding words in Word

Word’s search function is an easy way to get the count for a specific word or phrase, but it’s limited to one word or phrase at a time. If you want to check the number of multiple words, VBA can help you. The sub-procedure in List A can be a little intimidating if you’re not familiar with VBA, but don’t worry. Now let’s add the code to a Word document file.

List A

Sub FindWord()

'Returns the count of a specific word or phrases in the current document.

Dim intWordCount As Integer

Dim i As Integer

Dim w As Range

Dim strResponse As String

Dim intCount As Integer

On Error GoTo ErrHandler

'Sets For counter to the number of words in the current document.

intWordCount = ActiveDocument.Words.Count

For i = 0 To intWordCount

'Use InputBox() to solicit word or phrase.

strResponse = InputBox("Enter word", "Word count")

'Catches empty string in InputBox and quits procedure.

If strResponse = "" Then

Exit Sub

'Cycles through Words collection and compares each word in document

'to user input value, stored as strResponse.

Else

'Resets occurrence counter to 0.

intCount = 0

'Compares current word in document to user input value

'stored in strResponse.

For Each w In ActiveDocument.Words

If Trim(w.Text) = Trim(strResponse) Then intCount = intCount + 1

Next

MsgBox strResponse & "occurs " & intCount & " times.", vbOKOnly

End If

'Reduces For counter by 1.

intWordCount = intWordCount - 1

Next

Set w = Nothing

Exit Sub

ErrHandler:

MsgBox Err.Number & " " & Err.Description

Set w = Nothing

End Sub

If you’re using a ribbon version, be sure to save the workbook as a macro-enabled file. If you are working in the menu version, you can skip this step.

To enter the procedure, press Alt + F11 to open the Visual Basic Editor. In the Project Explorer on the left, select the document’s ThisDocument. (If you have multiple Word files open, be sure to select the correct ThisDocument.) You can enter the code manually or import the downloadable .cls file. Additionally, the macro is found in downloadable .docm, .doc, and .cls files. If you enter the code manually, do not paste it from this webpage. Instead, copy the code into a text editor, and then paste that code into the ThisDocument module. This will remove any ghost web characters that might otherwise cause errors. You are now ready to perform the procedure.

How to run VBA procedure in Word

When you run the macro, it displays an input box where you enter a word you want to count, then opens a message box displaying the number of occurrences of the word you entered. This Word procedure has one limitation: it finds single words; it will find no sentences.

To run the Word sub-procedure, click the Developer tab. In the Code group, click Macros. In the resulting dialog, choose FindWord, as shown in Figure C and click Run. When Word displays the input box, enter the word video— case doesn’t matter, and I’ll explain that in a minute — and click OK.

Figure C

Select the Word FindWord sub-procedure.
Select the Word FindWord sub-procedure.

A message box almost immediately displays the number of occurrences, as shown in Figure D.
Figure D

The message box displays the number of occurrences.
The message box displays the number of occurrences.

Close the message box and Word displays the input box again. Enter professional. This time, the message box displays the number of times Professional appears in the document. This process will continue until you click Cancel in the input box.

Are you curious about how the procedure works?

How the Word VBA procedure works

The first lines are variable declarations. Then the code stores the word count of the document in the integer variable, intWordCount. The first For loop uses this variable to loop through each word in the document. At the very end of the procedure, the code subtracts 1 from the variable – it’s a kind of countdown. This is how the first For Loop knows when to stop.

The input box stores its value, the word you enter, as strResponse. If you enter nothing and close the input box or press Cancel, the Exit Sub statement exits the procedure. Otherwise, Word executes the If Else statement. First, Word sets the intCount variable to 0, this is an internal count that tracks the number of occurrences. The second For Loop is a For Each construct, and it loops through each word, represented by w, in the document. This variable is actually a Range object, which is required by Word’s collection. But in simple terms, w stands for every word in the document (Words collection). This is why the procedure cannot handle phrases – w is a single word.

Remember that strResponse is the word you are counting, w is the current word in the document, and the w.Text property is the actual content. Sometimes .Text and strResponse will contain a space character, which is handled by the TRIM() function.

If strResponse and w.Text are equal, Word adds 1 to intWords, the number of occurrences of strResponse, the user input value. This loop compares each word in the document to strResponse. When the loop runs out of words, it displays the intWords value, the number of times strResponse occurs in the document, in a message box.

The last counter instruction subtracts 1 from intWordCount and the process repeats, until it has gone through all the words in the document. This way the input box continues to solicit words, so you don’t have to run it every time. When you’re done, click Cancel in the input box.

Error handling is basic, so you’ll want to thoroughly test this Word sub-procedure using your files before putting it to work. With three ways to count words and phrases, you should always have a quick fix.

You are unlikely to want to use the Developer tab route to perform the procedure. If so, read How to Add Office Macros to QAT Toolbar for quick access.