Consulting

Results 1 to 4 of 4

Thread: Finding, counting, and highlighting matches from a list

  1. #1
    VBAX Regular
    Joined
    Oct 2022
    Posts
    27
    Location

    Finding, counting, and highlighting matches from a list

    I need to develop a macro to count occurrences of words in a document, and at the same time to apply coloured highlighting to them.

    The prototype below counts six words listed in an array ("apple", "orange", "carrot", "potato", "corn", "wheat") and displays the totals in a message box.

    Sub Macro1()
    Dim vFindText As Variant
    Dim oRng As Range
    Dim i As Integer, n(6) As Integer
        vFindText = Array("apple", "orange", "carrot", "potato", "corn", "wheat")
        For i = 0 To UBound(vFindText)
            Set oRng = ActiveDocument.Range
            With oRng.Find
                .Text = vFindText(i)
                Do While .Execute
                    n(i) = n(i) + 1
                    ' Add code here to apply highlighting?
                    oRng.Collapse 0
                Loop
            End With
        Next i
    lbl_Exit:
        MsgBox _
        "Fruit:" & vbCr & _
        " " & n(0) & " apple" & vbCr & _
        " " & n(1) & " orange" & vbCr & vbCr & _
        "Vegetable:" & vbCr & _
        " " & n(2) & " carrot" & vbCr & _
        " " & n(3) & " potato" & vbCr & vbCr & _
        "Grain:" & vbCr & _
        " " & n(4) & " corn" & vbCr & _
        " " & n(5) & " wheat" _
        , vbInformation
        Exit Sub
    End Sub
    How can I get it to apply coloured highlighting at the same time; for example, turquoise for fruit (i.e., "apple" or "orange", when i = 1 or 2), pink for vegetable (i.e., "carrot" or "potato", when i = 3 or 4), and green for grain (i.e., "corn" or "wheat", when i = 5 or 6)?

    I wondered whether it is possible to add something inside the Do loop, along the following lines, but I don't know the proper code for applying highlighting.

    If i = 1 Or 2 Then [Apply highlight wdTurquoise]
    Else If i = 3 Or 4 Then [Apply highlight wdPink]
    Else If i = 5 Or 6 Then [Apply highlight wdGreen]

    The counting works OK on the attached "Test before" document. The attached "Test after" document shows how it should look after the macro is run.

    Test before.docx
    Test after.docx

  2. #2
    How about
    ' Add code here to apply highlighting?
                    Select Case i
                        Case 0, 1
                            oRng.HighlightColorIndex = wdTurquoise
                        Case 2, 3
                            oRng.HighlightColorIndex = wdPink
                        Case 4, 5
                            oRng.HighlightColorIndex = wdGreen
                    End Select
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Regular
    Joined
    Oct 2022
    Posts
    27
    Location
    That works fine, thanks.

  4. #4
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,054
    Location
    @Harry88 if you have a solution to your thread, can you please use the thread tools option to mark the thread as "Solved"?
    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
  •