locked
Looking for Macro Help - counting occurrences of a string - after confirmation??? RRS feed

  • Question

  • Howdy, all.

    I'm trying to write a VBA macro where we search for the number of occurrences of a specific text string in a document.  However, there are certain places in the document where we don't want to "count" that text string as having occurred. 

    So is there a way to get the standard do-loop search macro to advance the display to the found element and confirm whether or not we want it included in the count?

    Example:  I'd like to integrate something like this into a string counter macro...

         .Execute FindText:=x
         ConfirmResponse = MsgBox("Do you want to include this in the count?", vbYesNo)
         If ConfirmResponse = vbYes Then
         y = y + 1
         End If 

    Am I just totally out of it on this one?  Can the macro-based string search on the document be taught to "scroll" along with the search?

    Thanks in advance!!!

    Tuesday, November 13, 2012 2:34 AM

Answers

  • Try this. Modify as needed.

    Sub CountWithConfirmation()
        Dim strFind As String
        Dim lngCount As Long
        strFind = "change"
        Selection.HomeKey Unit:=wdStory
        With Selection.Find
            ' Initialize
            .ClearFormatting
            .Text = strFind
            .Wrap = wdFindStop
            ' Change as needed
            .MatchCase = False
            .MatchWholeWord = True
            .MatchWildcards = False
            ' Loop through document
            Do While .Execute
                If MsgBox("Include this occurrence?", _
                        vbYesNo + vbQuestion) = vbYes Then
                    lngCount = lngCount + 1
                End If
            Loop
        End With
        ' Do something with the result
        MsgBox lngCount & " occurrences included.", vbInformation
    End Sub


    Regards, Hans Vogelaar

    • Marked as answer by Jaynet Zhang Monday, November 19, 2012 1:42 AM
    Tuesday, November 13, 2012 8:10 AM

All replies

  • Try this. Modify as needed.

    Sub CountWithConfirmation()
        Dim strFind As String
        Dim lngCount As Long
        strFind = "change"
        Selection.HomeKey Unit:=wdStory
        With Selection.Find
            ' Initialize
            .ClearFormatting
            .Text = strFind
            .Wrap = wdFindStop
            ' Change as needed
            .MatchCase = False
            .MatchWholeWord = True
            .MatchWildcards = False
            ' Loop through document
            Do While .Execute
                If MsgBox("Include this occurrence?", _
                        vbYesNo + vbQuestion) = vbYes Then
                    lngCount = lngCount + 1
                End If
            Loop
        End With
        ' Do something with the result
        MsgBox lngCount & " occurrences included.", vbInformation
    End Sub


    Regards, Hans Vogelaar

    • Marked as answer by Jaynet Zhang Monday, November 19, 2012 1:42 AM
    Tuesday, November 13, 2012 8:10 AM
  • Many thanks!  Looks good so far; out at remote site, will try to install later tonight. 
    Tuesday, November 13, 2012 11:49 PM