Results 1 to 10 of 10
  1. #1
    New Lounger
    Join Date
    Mar 2003
    Location
    Canada
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Count the # of occurrences of a particular word

    Is there a way to count the number of occurrences of a particular word within a Word 2000 document? (without using Find / Replace)

  2. #2
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Los Angeles Area, California, USA
    Posts
    7,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Count the # of occurrences of a particular wor

    Hi rdn:
    May I ask why you don't want to use Find/Replace, which, as I'm sure you know, can count a word without making any real changes?

  3. #3
    New Lounger
    Join Date
    Mar 2003
    Location
    Canada
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Count the # of occurrences of a particular wor

    User's request. Thanks for the response.

  4. #4
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Count the # of occurrences of a particular wor

    I suspect this would be very slow on a large document but you could use a macro like

    Public Function iCountWord(strWord As String) As Integer

    Dim rngWord As Range
    Dim iCount As Integer

    strWord = LCase(Trim(strWord))

    For Each rngWord In ActiveDocument.Words
    If LCase(Trim(rngWord)) = strWord Then iCount = iCount + 1
    Next rngWord

    iCountWord = iCount

    End Function

    I tested it by filling a word document with "The quick brown fox jumps over the lazy dog" and running this short test macro

    Public Sub TestMe()
    MsgBox iCountWord("quick")
    End Sub

  5. #5
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Count the # of occurrences of a particular wor

    Klaus,

    Your method appears to use the banned Find/Replace.

    The only reason I can think of for wanting to do this without using Find/Replace is if you need to maintain the context in the Find / Replace dialog box, if this guess is correct then we could store all the status for that dialog box in variables at the beginning of the Macro and restore them at the end.

    StuartR

  6. #6
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Count the # of occurrences of a particular wor

    Looping words is a bit slow... (edited: though not as slow as I though, after checking out Stuart's function)
    And you'll run into problems with phrases or anything Word doesn't recognize as one "word", such as hyphenated words.

    It'd probably be best to teach the users to use "Edit > Find" (Replace with: ^&), but if it has to be done with a macro, the one below should be acceptably fast (though it only searches in the main story range).

    I found the method in the Word newsgroups; sorry that I can't give proper credit, but I don't remember the author.
    The macro determines the number of characters, then replaces the word or phrase with itself plus some character, counts characters again, and takes the difference.

    It would probably make sense to add a loop over all story ranges to include footnotes, comments etc., but I wanted to keep it simple.

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16> Klaus


    <pre>Sub CountWordsOrPhrases()
    ' Change the search criteria like .MatchCase if you want
    Dim sWord As String
    Dim lengthStoryRange As Long
    lengthStoryRange = ActiveDocument.StoryRanges(wdMainTextStory).End
    sWord = InputBox("Please enter the word/phrase", "Count")
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
    .Text = sWord
    .Replacement.Text = ChrW(&HE000) & "^&"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = True
    .MatchWholeWord = True
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    MsgBox ActiveDocument.StoryRanges(wdMainTextStory).End _
    - lengthStoryRange & " occurrances in main text story", _
    vbInformation, _
    sWord
    ActiveDocument.Undo
    End Sub</pre>


  7. #7
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Count the # of occurrences of a particular wor

    Gosh, you are fast!

    And probably right, too. I also was baffled why "Find" was "banned", but didn't think of that explanation.

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16> Klaus

  8. #8
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Count the # of occurrences of a particular wor

    Instead of remembering/resetting all the "Find/Replace" settings, it would probably be easier to set a Range to the main text story
    <pre>Set rngSearch=ActiveDocument.Content</pre>

    and then use rngSearch.Find instead of Selection.Find.

    The macro's "Find/Replace" doesn't affect the user's settings then.

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16> Klaus

  9. #9
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Count the # of occurrences of a particular wor

    Thanks, Stuart. So the kudos should go to Bart Verbeek!

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16> Klaus

  10. #10
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Count the # of occurrences of a particular wor

    There is a version of this macro on the mvps web site titled<A target="_blank" HREF="http://www.mvps.org/word/FAQs/MacrosVBA/GetNoOfReplacements.htm">
    How to find out, using VBA, how many replacements Word made during a Find & Replace All
    Or: How to find out how many occurrences there are of a particular word in a document</A>

    StuartR

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •