3 ways to count the number of occurrences of a specific word or phrase in a Word document

On the Home tab, click Editing and choose Find from the dropdown.
In the resulting pane, go into video.

Image: prima91/Adobe Stock
Returning the number of times a word or phrase occurs in a Microsoft Word file is a typical task for lots of authors and editors. Word has 2 integrated methods to count incidents of a specific word. If thats not versatile enough, you can utilize a Word VBA sub treatment.
SEE: Software Installation Policy (TechRepublic Premium).

Returning the number of times a word or expression happens in a Microsoft Word file is a common job for many authors and editors. Word has 2 integrated ways to count occurrences of a particular word. The demonstration files are made up of 2 sets of the same text generated by Words RAND() function, but youll desire to download the presentation files for the Word VBA treatment. Words Find feature is a simple method to get the count of a specific word or phrase, but its restricted to one word or expression at a time. When Word displays the input box, get in the word video– case doesnt matter, and Ill describe that in a minute– and click OK.

Thats it! As you can see in Figure A, this feature shows the variety of times the word video happens in the present file.
Figure A.
Word shows 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.
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. Its hassle-free for browsing however as soon as you try to work in the file the highlights vanish.
Figure B.
This will show the words youre browsing for in Word by highlighting them.
Both methods are easy and quick, and they work in Word for the web. You do not require to work any harder if either one meets your needs. The one feature missing in Word for the web is the highlighting; it doesnt stay in the document as soon as you close the Find & & Replace dialog. If you need something a bit more versatile, you might need a Word VBA treatment.
A VBA treatment to discover words in Word.
Words Find feature is an easy way to get the count of a particular word or expression, but its restricted to one word or phrase at a time. Now, lets add the code to a Word document file.
Listing A.
Sub FindWord().
Returns the count of a specific word or phrases in the existing file
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 present file.
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 stops treatment.
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 present word in file to user input worth.
kept in strResponse.
For Each w In ActiveDocument.Words.
If Trim( w.Text) = Trim( strResponse) Then intCount = intCount + 1.
MsgBox strResponse & &” takes place” & 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.
Be sure to save the workbook as a macro-enabled file if youre using a ribbon version. If youre operating in the menu version, you can avoid this step.
To enter the treatment, press Alt + F11 to open the Visual Basic Editor. In the Project Explorer to the left, pick the documents ThisDocument. If you have more than one Word file open that you pick the right ThisDocument.), (Be careful You can go into the code manually or import the downloadable.cls file. In addition, the macro is in the downloadable.docm,. doc and.cls files. Dont paste from this web page if you go into the code manually. Instead, copy the code into a full-screen editor and then paste that code into the ThisDocument module. Doing so will eliminate any phantom web characters that might otherwise cause errors. Now youre prepared to run the treatment.
How to run the VBA treatment in Word.
When you perform the macro, it will show an input box, in which youll get in a word that you desire to count, and after that open a message box showing the number of occurrences of the word you got in. This Word treatment has one restriction: it finds single words; it wont discover phrases.
To run the Word sub procedure, click the Developer tab. In the Code group, click Macros. In the resulting dialog, select FindWord, as shown in Figure C and click Run. When Word shows the input box, get in the word video– case doesnt matter, and Ill describe that in a minute– and click OK.
Figure C.
Select the Word sub procedure FindWord.
A message box almost right away shows the variety of incidents, as shown in Figure D.Figure D.
The message box shows the number of incidents.
Dismiss the message box and Word shows the input box again. Go into expert. This time, the message box shows the number of times expert appears in the file. This procedure will continue till you click Cancel in the input box.
Are you curious how the treatment works?
How the Word VBA procedure works.
The code stores the number of words in the file to the integer variable, intWordCount. The very first For loop uses that variable to work its method through every word in the document.
The input box stores its worth, the word that you enter, as strResponse. The second For loop is a For Each construct, and it cycles through every word, represented by w, in the document. Simply put, w represents every word in the document (Words collection).
Keep in mind that strResponse is the word youre counting, w is the present word in the w.text and the document home is the real content. Sometimes.Text and strResponse will include an area character, which is dealt with by the TRIM() function.
Word adds 1 to intWords– the number of occurrences of strResponse– the users input worth if strResponse and w.Text are equivalent. This loop compares every word in the document to strResponse. When the loop runs out of words, it shows the intWords value, the number of times strResponse happens in the document, in a message box.
The last counter statement subtracts 1 from intWordCount, and the procedure repeats itself– continuing till it has actually cycled through all the words in the document. In this way, the input box continues to obtain words, so you dont have to perform it each time. When youre done, click Cancel in the input box.
The error handling is basic, so youll desire to test this Word sub treatment completely utilizing your files prior to putting it to work. With three methods to count expressions and words, you must always have a fast option.
Its unlikely that youll want to utilize the Developer tab route to execute the treatment. If so, check out How to include Office macros to the QAT toolbar for fast access.

More about Software.

I recommend that you hold off on updating to Windows 11 until all the kinks are worked out. The demonstration files are comprised of 2 sets of the exact same text produced by Words RAND() function, however youll desire to download the presentation files for the Word VBA procedure. Word for the web doesnt support VBA, however it does support a built-in technique.
About Words Find feature.
If a response on the fly is good enough, then Words Find feature is the method to go. Lets browse the presentation file for the word “video” as follows:.


Leave a Comment