Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / May 2008

Tip: Looking for answers? Try searching our database.

Programmatically count instances of a word

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Spicelon - 08 May 2008 03:51 GMT
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-
Doug Robbins - Word MVP - 08 May 2008 07:36 GMT
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.

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

> Word 2002
> Windows XP Pro SP2
[quoted text clipped - 4 lines]
>
> -gk-
Klaus Linke - 09 May 2008 11:56 GMT
> 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
Sandusky - 13 May 2008 22:54 GMT
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
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.