
Signature
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
> 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
But what I want, actually what I _need_, is the actual count - preferably in
VBA.
-gk-
> 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
[quoted text clipped - 8 lines]
>>
>> -gk-
Doug Robbins - Word MVP - 14 May 2008 20:44 GMT
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."

Signature
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
> But what I want, actually what I _need_, is the actual count - preferably
> in VBA.
[quoted text clipped - 13 lines]
>>>
>>> -gk-
Klaus Linke - 15 May 2008 10:42 GMT
> 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