3 ways to count the number of occurrences of a specific word or phrase in a Word document
Image: prima91/Adobe Stock

Returning the number of times a word or phrase occurs 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 you’ve entered a client’s name frequently when you’re working with marketing material. Fortunately, Word has two built-in ways to count occurrences of a specific word. If that’s not flexible enough, you can use a Word VBA sub procedure. In this article, I’ll show you both built-in methods and a procedure.

SEE: Software Installation Policy (TechRepublic Premium)

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

About Word’s Find feature

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

  1. On the Home tab, click Editing and choose Find from the dropdown.
  2. In the resulting pane, enter video.

That’s it! As you can see in Figure A, this feature displays the number of times the word video occurs in the current document.

Figure A

Word displays the count in the Navigation pane.
Word displays the count in the Navigation pane.

In addition, you can click the links below to access each occurrence. As long as the Find feature is active, the yellow highlighting remains. When you close the Navigation pane, the highlights disappear.

As easy as that is, there’s a second way. Press Ctrl + H or choose Replace from the Editing dropdown and click the Find tab. Enter video in the Find What control, click Reading Highlights and then select Highlight All. Figure B shows similar results. The big difference is that the highlighting remains when you close the Find & Replace dialog. It’s convenient for browsing but as soon as you try to work in the document the highlights disappear.

Figure B

This will show the words you're searching for in Word by highlighting them.
This will show the words you’re searching for in Word by highlighting them.

Both methods are quick and easy, and they work in Word for the web. If either one meets your needs, you don’t need to work any harder. The one feature missing in Word for the web is the highlighting; it doesn’t remain in the document once you close the Find & Replace dialog. If you need something a bit more flexible, you might need a Word VBA procedure.

A VBA procedure to find words in Word

Word’s Find feature is an easy way to get the count of a specific word or phrase, but it’s limited to one word or phrase at a time. If you want to check the count of more than one word, VBA can help. The sub procedure in Listing A might be a bit intimidating if you’re not familiar with VBA, but don’t worry. Now, let’s add the code to a Word document file.

Listing 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’re 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 to the left, select the document’s ThisDocument. (Be careful if you have more than one Word file open that you select the right ThisDocument.) You can enter the code manually or import the downloadable .cls file. In addition, the macro is in the downloadable .docm, .doc and .cls files. If you enter the code manually, don’t paste from this web page. Instead, copy the code into a text editor and then paste that code into the ThisDocument module. Doing so will remove any phantom web characters that might otherwise cause errors. Now you’re ready to run the procedure.

How to run the VBA procedure in Word

When you execute the macro, it will display an input box, in which you’ll enter a word that you want to count, and then open a message box displaying the number of occurrences of the word you entered. This Word procedure has one limitation: it finds single words; it won’t find phrases.

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 sub procedure FindWord.
Select the Word sub procedure FindWord.

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.

Dismiss 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 how the procedure works?

How the Word VBA procedure works

The first several lines are variable declarations. Then, the code stores the number of words in the document to the integer variable, intWordCount. The first For loop uses that variable to work its way through every word in the document. At the very end of the procedure, the code subtracts 1 from the variable—it’s a countdown of sorts. That’s how the first For loop knows when to stop.

The input box stores its value, the word that you enter, as strResponse. If you enter nothing and close the input box or press Cancel, the Exit Sub statement quits the procedure. Otherwise, Word executes the If statement’s Else. First, Word sets the intCount variable to 0—this is an inner count that keeps up with the number of occurrences. The second For loop is a For Each construct, and it cycles through every word, represented by w, in the document. This variable is actually a Range object, which is required by Word’s collection. But simply put, w represents every word in the document (Words collection). This is why the procedure can’t handle phrases—w is a single word.

Remember that strResponse is the word you’re 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’s input value. This loop compares every 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 statement subtracts 1 from intWordCount, and the process repeats itself—continuing until it has cycled through all the words in the document. In this way, the input box continues to solicit words, so you don’t have to execute it every time. When you’re done, click Cancel in the input box.

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

It’s unlikely that you’ll want to use the Developer tab route to execute the procedure. If so, read How to add Office macros to the QAT toolbar for quick access.

Source link

LEAVE A REPLY

Please enter your comment!
Please enter your name here