Results 1 to 7 of 7
  1. #1
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Virginia, USA
    Posts
    1,560
    Thanks
    37
    Thanked 1 Time in 1 Post

    Count instances of a word (2003)

    I need to count the number of times a particular word appears in a single fairly large document. I've used Find (checking the box to highlight, then counting all the highlighted words), but that's a boring and inaccurate method. Anyone have a macro to do this?

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Count instances of a word (2003)

    You could replace the word with "TEMP" and Word should report the number of replacements. Then remember to Undo. <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

    (I'm sure there are other ways. I think we've had threads on this.)

  3. #3
    Platinum Lounger
    Join Date
    Feb 2002
    Location
    A Magic Forest in Deepest, Darkest Kent
    Posts
    5,681
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Count instances of a word (2003)

    Lucas

    No need for code, follow these steps:

    1) Press Ctrl+H to display the Replace tab of the Find and Replace dialog box.
    2) In the Find What box, enter the word or phrase you want counted.
    3) In the Replace With box, enter ^&.

    This character sequence tells Word that you want to replace what you find with whatever you placed in the Find What box. (In other words, you are replacing the word or phrase with itself.)
    Jerry

  4. #4
    Platinum Lounger
    Join Date
    Feb 2002
    Location
    A Magic Forest in Deepest, Darkest Kent
    Posts
    5,681
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Count instances of a word (2003)

    alternatively you could use this code:

    <pre>Sub FindWords()
    Dim sResponse As String
    Dim iCount As Integer

    ' Input different words until the user clicks cancel
    Do
    ' Identify the word to count
    sResponse = InputBox(Prompt:="What word do you want to count?", _
    Title:="Count Words", Default:="")

    If sResponse > "" Then
    ' Set the counter to zero for each loop
    iCount = 0
    Application.ScreenUpdating = False
    With Selection
    .HomeKey Unit:=wdStory
    With .Find
    .ClearFormatting
    .Text = sResponse
    ' Loop until Word can no longer
    ' find the search string and
    ' count each instance
    Do While .Execute
    iCount = iCount + 1
    Selection.MoveRight
    Loop
    End With
    ' show the number of occurences
    MsgBox sResponse & " appears " & iCount & " times"
    End With
    Application.ScreenUpdating = True
    End If
    Loop While sResponse <> ""
    End Sub</pre>

    Jerry

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

    Re: Count instances of a word (2003)

    There is a much faster method here on the MVPS web site.

    StuartR

  6. #6
    Platinum Lounger
    Join Date
    Feb 2002
    Location
    A Magic Forest in Deepest, Darkest Kent
    Posts
    5,681
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Count instances of a word (2003)

    Looking good, wouldn't want to argue with Bob <img src=/S/grin.gif border=0 alt=grin width=15 height=15>
    Jerry

  7. #7
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Virginia, USA
    Posts
    1,560
    Thanks
    37
    Thanked 1 Time in 1 Post

    Re: Count instances of a word (2003)

    Thank you, Jerry, and everyone for the help!

Posting Permissions

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