Advanced Find questions--Word 2010

User avatar
kdock
5StarLounger
Posts: 721
Joined: 21 Aug 2011, 21:01
Location: The beautiful hills of Western North Carolina

Advanced Find questions--Word 2010

Post by kdock »

I want to use some snazzy vba to find variations on a phrase. I notice that when you do a search in the navigation pane, it not only highlights all the hits throughout the document, but also tells me how many hits there are.

I want to find the variations and compare the number of matches. Does anyone know if there's a way to capture the number of matches? I have very little time today and my searching skills have been severely impaired by my exasperation.
Thanks much! Kim :bananas:
"Hmm. What does this button do?" Said everyone before being ejected from a car, blown up, or deleting all the data from the mainframe.

User avatar
HansV
Administrator
Posts: 78586
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Advanced Find questions--Word 2010

Post by HansV »

There is no VBA equivalent of "Find All" etc. You could use this function (it will search the body of the document only:

Code: Select all

Function NumFound(strFind As String, Optional blnMatchWholeWord As Boolean, _
        Optional blnMatchCase As Boolean, Optional blnMatchWildCards As Boolean)
    Dim i As Long
    With ActiveDocument.Content.Find
        .ClearFormatting
        .Text = strFind
        .MatchWholeWord = blnMatchWholeWord
        .MatchCase = blnMatchCase
        .MatchWildcards = blnMatchWildCards
        Do While .Execute
            i = i + 1
        Loop
        NumFound = i
    End With
End Function
Examples of usage:

Debug.Print NumFound("the")

will return the number of occurrences of "the", whether as a whole word or as part of a longer word such as "theater" or "atheist".

Debug.Print NumFound("the", True)

will return the number of occurrences of "the" as a separate word.

Debug.Print NumFound("t?e", , , True)

will return the number of occurrences of t and e with an arbitrary character in between, e.g. "the" or "tre" or "twe".
Best wishes,
Hans

User avatar
kdock
5StarLounger
Posts: 721
Joined: 21 Aug 2011, 21:01
Location: The beautiful hills of Western North Carolina

Re: Advanced Find questions--Word 2010

Post by kdock »

Oh, Hans. I don't think (or can't think, to be more precise), that this will work for me today. I'm going to go down and dirty, and worry about the cleanup tomorrow.

Thank you thank you for responding so quickly! Best, Kim
"Hmm. What does this button do?" Said everyone before being ejected from a car, blown up, or deleting all the data from the mainframe.