none
Find and highlight specific text in MS Word RRS feed

  • Question

  • Hello All,

    Is there any macro to "Find and highlight specific text in MS Word" and alerting through a message pop up box in a active word document.

    Also the message box should indicates "if there is no such speific text in the active document".

    Regards

    John

    Wednesday, November 6, 2019 3:30 AM

Answers

  • Hi John,

    Code would be like this:
    ' --- HighLight if a certain text is in this document ---
    Sub Highlight_Text()
        ' --- set cursor at the top of this document
        ActiveDocument.Range(0, 0).Select
        ' --- Find a specific text
        Dim cnt As Integer: cnt = 0
        Dim targetText As String
        targetText = "John"
        With Selection.Find
            .Text = targetText
            Do While .Execute
                Selection.Range.HighlightColorIndex = wdYellow
                cnt = cnt + 1
            Loop
        End With
        ' ---
        MsgBox "Highlight completed !!" & vbCrLf & _
                "[" & targetText & "] appears " & cnt & " times."
    End Sub
    

    Regards,

    Ashidacchi -- http://hokusosha.com

    • Marked as answer by Johnmiller1234 Thursday, November 7, 2019 2:49 AM
    Wednesday, November 6, 2019 9:42 AM

All replies

  • Hi John,

    I've made a sample.
        Find text "John" and make it highlight (Yellow).
        

    Sub Highlight_Text()
        ' --- set cursor at the top of this document
        ActiveDocument.Range(0, 0).Select
        ' --- Find "John" in all text
        With Selection.Find
            .Text = "John"
            Do While .Execute
                Selection.Range.HighlightColorIndex = wdYellow
            Loop
        End With
        ' ---
        MsgBox "Highlight completed !!"
    End Sub
    
    Regards,

    Ashidacchi -- http://hokusosha.com

    Wednesday, November 6, 2019 8:44 AM
  • Hi Ashidacchi,

    It works good, however the message box shows "Highlight completed" even the specific text not in the active document.

    Also could the message box also alert the number of specific text found in the active document.


    John

    Wednesday, November 6, 2019 9:01 AM
  • Hi John,

    Code would be like this:
    ' --- HighLight if a certain text is in this document ---
    Sub Highlight_Text()
        ' --- set cursor at the top of this document
        ActiveDocument.Range(0, 0).Select
        ' --- Find a specific text
        Dim cnt As Integer: cnt = 0
        Dim targetText As String
        targetText = "John"
        With Selection.Find
            .Text = targetText
            Do While .Execute
                Selection.Range.HighlightColorIndex = wdYellow
                cnt = cnt + 1
            Loop
        End With
        ' ---
        MsgBox "Highlight completed !!" & vbCrLf & _
                "[" & targetText & "] appears " & cnt & " times."
    End Sub
    

    Regards,

    Ashidacchi -- http://hokusosha.com

    • Marked as answer by Johnmiller1234 Thursday, November 7, 2019 2:49 AM
    Wednesday, November 6, 2019 9:42 AM
  • Thank you Ashidacchi,

    It works great.

    Regards

    John

    Thursday, November 7, 2019 2:52 AM
  • HI Ashidacchi,

    One more request, no message box required if specific text not in the document.

    John


    Thursday, November 7, 2019 1:17 PM
  • Hi John,

    If you have no need of message box, you should only remove "MsgBox" line or make it commented.

    I'm afraid you need to write basic code by yourself. 

    Regards,

    Ashidacchi -- http://hokusosha.com

    Thursday, November 7, 2019 10:34 PM