Results 1 to 9 of 9

Thread: Solved: How to count occurences of a string in a doc?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #6
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,710
    Location
    Or...

    Dim r As Range
    Dim j As Long
    Set r = ActiveDocument.Range
    With r.Find
       .Text = InputBox("What word(s)?")
       Do While .Execute(Forward:=True) = True
           j = j + 1
        Loop
    End With
    MsgBox "Given word(s) was found " & j & " times."
    No error trapping on the input string. If there is no string found (because of spelling errors for example) the message will be "0 times"
    Last edited by Aussiebear; 03-19-2023 at 04:23 PM. Reason: Updated code tags

Posting Permissions

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