Programmatically count instances of a word

S

Spicelon

Word 2002
Windows XP Pro SP2

Suppose I want to count the instances of the word "planet" in a document. I
know I can use the Find... function under the Edit menu, but is there a
[fast] way to do it from VBA?

-gk-
 
D

Doug Robbins - Word MVP

The quickest way would be to use Edit>Replace to replace "planet" with
"planet" At the very least it saves the time of writing the code to do it
in some other way.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
K

Klaus Linke

Doug Robbins - Word MVP said:
The quickest way would be to use Edit>Replace to replace "planet" with
"planet" At the very least it saves the time of writing the code to do it
in some other way.

If you need the result in a macro, maybe check out
http://word.mvps.org/FAQs/MacrosVBA/GetNoOfReplacements.htm

In some cases, it might be quicker to get the text into a string, and count
the number of occurrences there:

Dim sText As String
sText = ActiveDocument.Content.Text

Dim sWord As String
sWord = "planet"

Dim iNum As Long
iNum = UBound(Split(sText, sWord))

' or make it case-insensitive:
iNum = UBound(Split(UCase(sText), UCase(sWord)))

Debug.Print sWord & " appears " & iNum & " times."

Getting the text into the string takes some time, so that approach likely
does not pay off if you just need to count the number of times *one* word
appears.
But if you need to do that for many words though, or need to fetch lots of
other info that you can get directly from the string, it's often quicker.

Regards,
Klaus
 
S

Sandusky

But what I want, actually what I _need_, is the actual count - preferably in
VBA.

-gk-


Doug Robbins - Word MVP said:
The quickest way would be to use Edit>Replace to replace "planet" with
"planet" At the very least it saves the time of writing the code to do it
in some other way.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

Spicelon said:
Word 2002
Windows XP Pro SP2

Suppose I want to count the instances of the word "planet" in a document.
I know I can use the Find... function under the Edit menu, but is there a
[fast] way to do it from VBA?

-gk-
 
D

Doug Robbins - Word MVP

Dim i As Long
Dim findword As String
findword = InputBox("Enter the word that you want to find.", "Word
Counter")
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(Findtext:=findword, Forward:=True,
MatchWholeWord:=True, _
MatchCase:=True, MatchWildcards:=False, Wrap:=wdFindStop) =
True
i = i + 1
Selection.Collapse wdCollapseEnd
Loop
End With
Selection.HomeKey wdStory
MsgBox "The word " & findword & " appears in the document " & i & "
times."


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

Sandusky said:
But what I want, actually what I _need_, is the actual count - preferably
in VBA.

-gk-


Doug Robbins - Word MVP said:
The quickest way would be to use Edit>Replace to replace "planet" with
"planet" At the very least it saves the time of writing the code to do
it in some other way.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

Spicelon said:
Word 2002
Windows XP Pro SP2

Suppose I want to count the instances of the word "planet" in a
document. I know I can use the Find... function under the Edit menu, but
is there a [fast] way to do it from VBA?

-gk-
 
K

Klaus Linke

Sandusky said:
But what I want, actually what I _need_, is the actual count -
preferably in VBA.

Hi Sandusky,

I had posted two ways to do this quickly already... Don't you see the post?

Regards,
Klaus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top