Count specific word

  • Thread starter Thread starter Quency
  • Start date Start date
Q

Quency

I'd like to see how many times the same word has beed used
in the document. How do I do that? Word count only gives
all characters used, not specific one.

Thanks for your feedback!
 
Quency,

You can do this with find and replace. Say you want to find "Dim" from the
text below. Type Dim in the Find Field and Dim in the Replace Field and then
replace all. Word will display a report of the number of replacements made.
There is also a macro which will find occurences in the the main story text.
See below:



Sub CountOccurrences()



Dim iCount As Long

Dim strSearch As String



strSearch = InputBox$("Type in the word or phrase that you want to find and
count.")

iCount = 0



With ActiveDocument.Content.Find

.Text = strSearch

.Format = False

.Wrap = wdFindStop

Do While .Execute

iCount = iCount + 1

Loop

End With



If iCount = 1 Then

MsgBox Chr$(34) & strSearch & Chr$(34) & " was found " & _

iCount & " time."

End If

If iCount > 1 Then

MsgBox Chr$(34) & strSearch & Chr$(34) & " was found " & _

iCount & " times."

End If

End Sub
 
Greg:

Thank you so much for your answer. I love your macro. It
works like magic.

Thanks again.

Quency
 
Quency,

Not mine. Monkey see, monkey do :-)

Remember, it searches main story text only and will not detect or count
words in other stories e.g., headers/footers, endnotes, etc.
 
This isn't exactly in the rule book, but I would very
carefully do a find and replace with XXXX then hit
replace all and see how many it replaces. Follow this
very quickly with undo.
 
Here's a macro that does the same thing without having to type the word
or text into an input box. Just place the cursor in the word or select
the word, then run the macro:

Larry

Sub TextCountQuick()

' Counts current word or selected word or string.

Dim myrange As Range
Dim myPhrase As String
Dim i As Long

Application.ScreenUpdating = False
System.Cursor = wdCursorIBeam
Set myrange = ActiveDocument.Range

' If there is no selection, select current word.
If Selection.Type <> wdSelectionNormal Then Selection.Words(1).Select
' Unselect any empty space after text.
Selection.MoveEndWhile cset:=" ", Count:=wdBackward
myPhrase = Selection.Text
Selection.Collapse wdCollapseStart

myrange.Find.ClearFormatting
myrange.Find.Replacement.ClearFormatting
With myrange.Find
.Text = myPhrase
.Forward = True
.MatchWholeWord = False
.MatchWildcards = False
.Wrap = wdFindforward
Do While .Execute
i = i + 1
Loop
End With

MsgBox "Occurrences of '" & myPhrase & "' " & i, , "Text Count"

' clear Find
myrange.Find.Text = ""

End Sub
 
Back
Top