Consulting

Results 1 to 5 of 5

Thread: Output of Number of Hits in a Search Macro

  1. #1

    Output of Number of Hits in a Search Macro

    Using Ctrl-F in a Word document opens a new search box, that will show each of the hits for an entered keyword. It also lists X results, where X = the number of hits.
    My question is: What code can be used to capture that X quantity in a VBA macro, in order to inform the user how many results were found for a search macro?

  2. #2
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    413
    Location
    Bing search: "word vba search for string count" has couple results including AI generated code. Check this out https://stackoverflow.com/questions/...rd-occurrences
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,372
    Location
    Maybe try this:

    Sub CountWordOccurrences()
        Dim WordToFind As String
        Dim WordCount As Long  Dim i As Long
        Dim FoundRange As Range
        ' Get the word to find from the user
        WordToFind = InputBox("Enter the word to find:", "Find Word")
        ' Exit if the user cancels the input box
        If WordToFind = "" Then 
            Exit Sub
            ' Make the word case-insensitive for the search
            WordToFind = LCase(WordToFind)
            ' Loop through each word in the document
            For i = 1 To ActiveDocument.Words.Count
                ' Check if the current word matches the word to find
                If LCase(ActiveDocument.Words(i)) = WordToFind Then
                    WordCount = WordCount + 1
                End If
            Next i
            ' Display the result in a message box
            MsgBox "The word '" & WordToFind & "' occurs " & WordCount & " times in the document.", vbInformation
        End If
    End Sub
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,411
    Location
    Quote Originally Posted by R. Racoon View Post
    Using Ctrl-F in a Word document opens a new search box, that will show each of the hits for an entered keyword. It also lists X results, where X = the number of hits.
    My question is: What code can be used to capture that X quantity in a VBA macro, in order to inform the user how many results were found for a search macro?
    Sub Test()
        MsgBox CountWordOccurrences("Test")
    End Sub
    
    Function CountWordOccurrences(strText)
        Dim lngIndex As Long
        Dim oRng As Range
        Set oRng = ActiveDocument.Range
        With oRng.Find
            .Text = strText
            .MatchCase = False
            .Forward = True
            .Wrap = wdFindStop
            While .Execute
                lngIndex = lngIndex + 1
                oRng.Collapse wdCollapseEnd
            Wend
            CountWordOccurrences = lngIndex
        End With
        lbl_Exit:
        Exit Function
    End Function
    Last edited by Aussiebear; 01-28-2025 at 03:15 PM.
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,372
    Location
    Hmmm.... i was close but no cigar.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Posting Permissions

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