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:
- On the Home tab, click Editing and choose Find from the dropdown.
- 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
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
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
A message box almost immediately displays the number of occurrences, as shown in Figure D.
Figure D
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.
Word count refers to the total number of words in a document. Counting your words might seem very obvious because as you type, you can see your count increase with each character on the status bar. However, sometimes your word count might come with specific details, such as ‘Endnotes and footnotes are not included in the counting.’ Some professors might require you to exclude these and will look out for them as they grade your work.
This article will be a guide on how to count only the words in a specific section.
1. Select that portion of the text with your mouse or keyboard
Highlight the entire text or section you want to count.
2. Select the ‘Review’ tab on the ribbon
Go to ‘Review’ on the ribbon. When you select it, you will see ‘Word Count’ in the top left under ‘Thesaurus.’
3. Open the Word Count dialog box to see how many words are in that section.
It is important to note that Word Count can include or exclude footnotes, endnotes, and text boxes. However, it is an ‘all or nothing’ kind of feature. Either you include all footnotes, endnotes and textboxes, or none.
A dialog box will appear.
At the bottom, you will see a box next to ‘Include text boxes, footnotes, and endnotes.’ If it is checked, uncheck it.
Above it, you will see statistics with numbers next to them: pages, words, characters (no spaces), characters (spaces), paragraphs, lines.
Now you know how many words are in that particular section.
Are you a student looking for an easy and convenient way to do group projects without having to physically meet your groupmates or keep exchanging documents one after the other, as you make edits?
You do not know which document you worked on most recently. You are wasting time re-typing all your work, copying and pasting. With group projects, a groupmate might reformat your paper into a format that probably cannot open on your phone or laptop. All these barriers are very exhausting.
Simul Docs is the solution for you.
The first step is downloading Simul Docs then you can get started.
After writing your first draft in Microsoft Word, upload it to Simul Docs. This will automatically make it the first version. Now, another colleague can open it and make their edits. This will create another version different from the first.
Whenever another person opens a version and makes edits, a new version is created with the latest changes.
Sometimes, you might be in a rush and forget to turn on tracked changes so your modifications can be updated. You have nothing to worry about. Simul Docs automatically records these edits regardless of your online or offline status.
Since it is a collaboration, a bonus feature is the ability to add comments to the document. If you want a colleague to review the changes by a specific time, or any other instructions, you can let them know in the comment section. Later on, these comments can be removed after your groupmates have seen and executed the instructions.
Additionally, you and your groupmates can work on the document at the same time. There’s no need to lazy around waiting for someone else to make edits. You might do your part in time, and the last person ends up costing the group credit because they work slowly. You can save time by working on a document at once, and Simul Docs keeps each version separately.
Let us say another co-worker is supposed to include their input, but they do not have Simul Docs. Maybe they are not tech-savvy. All you have to do is download the document or forward it by email. Alternatively, you can upload it to any storage software like one drive, and once their changes have been made, they can forward it to you once again, and you can open it in Simul Docs.
Once all the necessary edits have been made, you can save the final version containing all the modifications with one click.
As you can see, with Simul Docs, you do not need many documents open to work on a single assignment or have to be in the exact location as your groupmates.
You and your groupmates can work on the same document from the comfort of your homes or workplaces.
Simul is the most convenient collaborative tool yet. Centralising your written project avoids mishaps like losing your work or losing its original format.
Visit simuldocs.com and simplify group edits today.
Service description
1Text.com Word Count is totally unique without any equivalents on the Web. Highlighting watery fragments, keyword density areas, and keywords in a text make the analysis interactive and easy to interpret.
Word Count includes:
Word count, character count in your text online
The online service count words and characters in your text, including or excluding spaces.
Analysis of keywords and semantic core of a text
Finding keywords and detection of theiк number is extremely useful for writing new texts and improving existing ones. The service arranges keywords in groups and by frequency for fast and simple search. It will also show keyword forms when you click on it.
Detection of watery in the text
Shows the exact percentage of stop-words, idioms and conjunctions it the text. A small percentage of watery in the text is natural, but:
- to 15% — average amount of watery;
- from 15% to 30% — exceeded amount;
- from 30% — high amount of watery in the text.
Detection of keywords density of the text
Keyword density shows a number of keywords in a text. The more the keywords, the higher the keyword density:
- to 30% — absence or average number of keywords in a text;
- from 30% to 60% — SEO-optimized text. Search engines consider such texts relevant to the keywords they include in most cases.
- from 60% — too optimized text with high keyword density.
Search for words with swapped letters
The parameter finds words consisting of letters from different alphabets: for example, the English word «table», where an «a» is taken from the Cyrillic alphabet. Some copywriters swap letters in the texts to pass Plagiarism Check successfully and raise the percentage of originality. Word Count 1Text.com will detect such words easily.
I am looking for a way to count certain words on a string.
$string = "How are you doing today? You are such a nice person!";
My goal is to count how many «are» in the $string variable.
Desire result: 2
asked Dec 14, 2013 at 7:07
4
Try this:
var count = yourString.match(/bareb/g);
count = count? count.length : 0; //checking if there are matches or not.
console.log(count);
answered Dec 14, 2013 at 7:27
Mr_GreenMr_Green
40.4k43 gold badges159 silver badges267 bronze badges
2
var string = "are you bare footed?";
console.log(string.split(/bareb/).length - 1);
answered Dec 14, 2013 at 7:14
thefourtheyethefourtheye
231k52 gold badges451 silver badges494 bronze badges
5
var str = "How are you doing today? You are such a nice person!";
var matchs;
matchs = str.match(/are/gi);
alert(matchs.length);
try this javascript match it returns match string as array format, and length method returns how many array index match.
answered Dec 14, 2013 at 7:15
Man ProgrammerMan Programmer
5,2802 gold badges21 silver badges21 bronze badges
try below.
var temp = "How are you doing today? You are such a nice person!.";
var count = countOcurrences(temp,"are");
alert(count);
function countOcurrences(str, value){
var regExp = new RegExp(value, "gi");
return str.match(regExp) ? str.match(regExp).length : 0;
}
answered Dec 14, 2013 at 7:15
Try this:
$string = "How are you doing today? You are such a nice person!";
var count = $string.match(/are/g);
alert(count.length);
answered Dec 14, 2013 at 7:26
Mr.GMr.G
3,3432 gold badges16 silver badges20 bronze badges
Try this:
var string = "How are you doing today? You are such a nice person!";
var num = string.match(/are/g).length;
alert(num);
Here is Demo
answered Dec 14, 2013 at 7:38
RingoRingo
3,7573 gold badges22 silver badges37 bronze badges
You can also try something like this:
var temp = "How are you doing today? You are such a nice person!.";
var arr = [];
for(var i = 0;i< temp.split(' ').length; i++)
{
if(temp.split(' ')[i] === "are"){
arr.push(temp.split(' '));
}
}
alert(arr.length)
answered Dec 14, 2013 at 7:49
defau1tdefau1t
10.6k2 gold badges34 silver badges46 bronze badges
The word count is the number of words in a document or passage of text. Word counting may be needed when a text is required to stay within certain numbers of words. This may particularly be the case in academia, legal proceedings, journalism and advertising. Word count is commonly used by translators to determine the price for the translation job. Word counts may also be used to calculate measures of readability and to measure typing and reading speeds (usually in words per minute). When converting character counts to words, a measure of 5 or 6 characters to a word is generally used for English.
Variations in the operational definitions of how to count the words can occur (namely, what «counts as» a word, and which words «don’t count» toward the total). However, especially since the advent of widespread word processing, there is a broad consensus on these operational definitions (and hence the bottom-line integer result). The consensus is to accept the text segmentation rules generally found in most word processing software (including how word boundaries are determined, which depends on how word dividers are defined). The first trait of that definition is that a space (any of various whitespace characters, such as a «regular» word space, an em space, or a tab character) is a word divider. Usually a hyphen or a slash is, too. Different word counting programs may give varying results, depending on the text segmentation rule details, and on whether words outside the main text (such as footnotes, endnotes, or hidden text) are counted. But the behavior of most major word processing applications is broadly similar.
However, during the era when school assignments were done in handwriting or with typewriters, the rules for these definitions often differed from today’s consensus. Most importantly, many students were drilled on the rule that «certain words don’t count», usually articles (namely, «a», «an», «the»), but sometimes also others, such as conjunctions (for example, «and», «or», «but») and some prepositions (usually «to», «of»). Hyphenated permanent compounds such as «follow-up» (noun) or «long-term» (adjective) were counted as one word. To save the time and effort of counting word-by-word, often a rule of thumb for the average number of words per line was used, such as 10 words per line. These «rules» have fallen by the wayside in the word processing era; the «word count» feature of such software (which follows the text segmentation rules mentioned earlier) is now the standard arbiter, because it is largely consistent (across documents and applications) and because it is fast, effortless, and costless (already included with the application).