There are 3 ways to count the number of words or phrases in a word

April 4, 2022 0 Comments

Display notebooks closeup Microsoft Word and Excel app.  Microsoft Office is an office suite of applications created by Microsoft.
Photo: prima91 / Adobe Stock

Returning how many 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 use extra words. On the other hand, you may want to make sure that you enter a client’s name frequently when you are dealing with marketing materials. Fortunately, Word has two built-in ways to calculate the occurrence of a particular word. If it is not flexible enough, you can use a Word VBA sub method. In this article, I will show you both the built-in method and a method.

Views: Software Installation Policy (TekriPublic Premium)

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

About the Word Find feature

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

  1. On the Home tab, click Edit and select Find from the dropdown.
  2. In the resulting pane, write Video.

That’s it! As you can see Figure AThis feature displays how many times the word video has occurred in the current document.

Figure A

Displays the count on the panel on the word.
Displays the count on the panel on the word.

Also, you can click on the links below to access each event. As long as the Find feature is active, there is yellow highlighting. When you close the navigation pane, the highlights disappear.

As simple as that is, there is a second way. Press Ctrl + H or select Replace from the Edit dropdown and click the Find tab. Enter Video In Find key controls, click Reading Highlights, and then select Highlight All. Figure B Shows similar results. The big difference is that when you find and close the replacement dialog, the highlighting goes away. It’s convenient to browse, but the highlights disappear as soon as you try to work on the document.

Figure B

It highlights the words you are looking for in Word.
It highlights the words you are looking for in Word.

Both methods are quick and easy, and they work in Word for the web. If one meets your needs, you don’t have to work harder. One feature missing from the Word for Web is highlighting; Once you find and close the replacement dialog it will not be in the document. If you need something a little more flexible, you may need a Word VBA approach.

A VBA method for finding words in Word

The word search feature is an easy way to calculate a specific word or phrase, but it is limited to one word or phrase at a time. If you want to test more than one word count, VBA can help. In the sub-method In the list If you’re not familiar with VBA, it can be a little intimidating, but don’t worry. Now, let’s add the code to a Word document file.

In the list

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.


'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


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

End If

'Reduces For counter by 1.

intWordCount = intWordCount - 1


Set w = Nothing

Exit Sub


MsgBox Err.Number & " " & Err.Description

Set w = Nothing

End Sub

If you use a ribbon version, be sure to save the workbook as a macro-enabled file. You can skip this step if you work in the menu version.

To access the method, press Alt + F11 to open Visual Basic Editor. In Project Explorer on the left, select ThisDocument in the document (Be careful if you have multiple Word files open that you select the correct ThisDocument.) You can write the code manually or import the downloadable .cls file. Also, macro downloadable .docm, .doc and .cls files contain If you enter the code manually, do not paste from this web page. Instead, copy the code to a text editor and then paste that code into this document module. Doing so will delete any Phantom Web characters that may otherwise cause errors. Now you are ready to run the procedure.

How to run VBA method in Word

When you run the macro, it will display an input box, where you will type a word you want to count and then open a message box showing the number of words you have entered. This word approach has one limitation: it finds single words; It can’t find the phrase.

To run the Word sub method, click on the Developer tab. In the Code group, click Macros. In the resulting dialog, select FindWord, as shown Figure C And click Run. When Word displays the input box, type the word Video-The case doesn’t matter, and I’ll explain it in a minute-and click OK.

Figure C

Select the Word sub method FindWord.
Select the Word sub method FindWord.

A message box displays the number of events almost instantly, as shown Figure D.
Figure D

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

Dismiss the message box and Word displays the input box again. Enter Professional. This time, the message box displays how many times the professional has appeared in the document. This process will continue until you click Cancel in the input box.

Are you interested in how the method works?

How the Word VBA method works

The first few lines are variable declarations. Then, the code stores the number of words in the document as an integer variable, intWordCount. The first for loop uses that variable to work through each word in the document. At the very end of the procedure, the code subtracts 1 from the variable — this is a different type of countdown. This way the first for loop knows when to stop.

The input box stores its value, the word you enter as strResponse. If you do not enter anything and close the input box or press Cancel, the exit sub statement statement is abandoned. Otherwise, the word if statement runs Else. First, Word sets the intCount variable to 0 — this is an internal calculation that holds the number of occurrences. The second for loop is one for each construction, and it rotates through each word represented by w in the document. This variable is actually a range object, which is required for the collection of Word. But simply put, every word in the document represents w (word collection). This is why the method cannot handle phrases: w is a single word.

Note that strResponse is the word you are counting, w is the current word in the document and w.Text attribute is the actual content. Sometimes .text and strResponse contain a space character, which is managed by the TRIM () function.

If strResponse and w.text are equal, add 1 to Word intWords – the number of occurrences of strResponse – user input value. This loop compares every word in the document to strResponse. When the word loop runs out, it displays the intWords value, how many times strResponse occurs in a document in a message box.

The last counter statement subtracts 1 from intWordCount, and repeats the process itself – it continues until it has circled through all the words in the document. That way, the input box keeps asking for words, so you don’t have to execute 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 method using your files before you start working. With three ways to count words and phrases, you should always have a quick solution.

It is unlikely that you will want to use the Developer tab root to run the process If so, read How to Add Office Macros to the QAT Toolbar for Quick Access.

Leave a Reply

Your email address will not be published.