Counting phrases using an MS Word Macro

October 18, 2011 at 05:56:32
Specs: Windows XP
The MS Word macro I copied and revised finds the number of occurrences of individual words. It currently includes:

Dim MyCount2 As an Integer
Dim MyWord2 As String
MyWord2 = “zoo”
If LCase(Trim(.Text)) = MyWord Then
MyCount = MyCount + 1
End If

This works great in the macro for individual words. I also need to find occurrences of a phrase, such as "zoo keeper" or "zoo keeper's helper". How may I change the current macro (see excerpts above) to find the number of occurrences of a phrase?

See More: Counting phrases using an MS Word Macro

October 18, 2011 at 09:13:25
As I said earlier, I don't do much with Word macros, so this is a guess. With the entire macro, perhaps I could provide more assiatnce.

Does this work?

MyWord2 = “zoo keeper's helper”

BTW...before posting any more code, please click on the following line and read the instructions found via that link.

Click Here Before Posting Data or VBA Code ---> How To Post Data or Code.

Report •

October 18, 2011 at 11:05:52
Thanks DerbyDad03. I tried that first off -- it returned no occurrences.

Below is the Macro, in all its glory! Any help would be greatly appreciated.

Sub Segment_Info_3()
' Segment_Info_3 Macro
' To find the occrances of multiple words and phrases in one MS Word document
   Dim MyCount As Integer, MyCount2 As Integer, MyCount3 As Integer
   Dim MyWord As String, MyWord2 As String, MyWord3 As String
   Dim Total As Integer
   Dim intI As Integer

    'Identify the segment words and phrases to search for.
MyWord = "zoo"
MyWord2 = "Zoo keeper"
MyWord3 = "Zoo keeper's helper"

   'Select the entire document

   'Count the total number of words in the document
   Total = Application.Selection.Range.Words.Count

   'Enumerate through each word selected, removing leading
   ' and trailing spaces
   For intI = 1 To Total

    With Selection.Words(intI)

         If LCase(Trim(.Text)) = MyWord Then
            MyCount = MyCount + 1
    End If
         If (LCase(.Text) & LCase(.Text)) = MyWord2 Then
            MyCount2 = MyCount2 + 1
    End If
         If LCase(Trim(.Text)) = MyWord3 Then
            MyCount3 = MyCount3 + 1
         End If

      End With

   Next intI

     'Display results

   Dim strMsg As String

strMsg = " The following Segment phrases were found in this document: " & vbNewLine & vbNewLine
strMsg = strMsg & " - " & MyCount & " occurrences of *" & MyWord & "* ;" & vbNewLine
strMsg = strMsg & " - " & MyCount2 & " occurrences of *" & MyWord2 & "* ; " & vbNewLine
strMsg = strMsg & " - " & MyCount3 & " occurrences of *" & MyWord3 & "*"
    MsgBox strMsg, vbInformation
End Sub

Report •
Related Solutions

Ask Question