Consulting

Results 1 to 5 of 5

Thread: Hightlight Keywords in a MS Word File

  1. #1

    Hightlight Keywords in a MS Word File

    Hi all,

    This is my first post!

    Just need one small help.
    I need to highlight some keywords in a word document.

    I need three inputs, instance handle,filepath of word doc,and the keywords to be highlighted in doc.
    I want multiple keywords to be passed in one go.

    Can you guys please help?

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    What do you mean by "instance handle"? This isn't standard Word terminology.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Hi Paul,

    Sorry for the confusion.

    Won't need instance handle as input anymore. Can you help me with the rest two?


    Cheers!

  4. #4
    This is fairly simple to achieve by using an array to hold the terms to find in quotes separated by commas e.g.

    Sub HighLightList()
    Dim vFindText As Variant
    Dim oRng As Range
    Dim i As Long
    vFindText = Array("lorem", "ipsum")    'the words or phrases to find
        For i = 0 To UBound(vFindText)
            Set oRng = ActiveDocument.Range
            With oRng.Find
                .ClearFormatting
                .Replacement.ClearFormatting
                Do While .Execute(findText:=vFindText(i), _
                                  MatchWholeWord:=True, _
                                  Forward:=True, _
                                  Wrap:=wdFindStop) = True
                    oRng.HighlightColorIndex = wdYellow
                    oRng.Collapse wdCollapseEnd
                Loop
            End With
            DoEvents
        Next
    lbl_Exit:
        Set oRng = Nothing
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  5. #5
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Try:
    Sub BulkHighlighter() Application.ScreenUpdating = False Dim j As Long, StrFnd As String, HiLt As Long HiLt = Options.DefaultHighlightColorIndex Options.DefaultHighlightColorIndex = wdBrightGreen StrFnd = InputBox("Insert your 'Find' terms with | delimiters, for example:" & vbCr & "the|quick|brown|fox") With ActiveDocument.Range.Find .ClearFormatting .Replacement.ClearFormatting .MatchWholeWord = True .MatchCase = False .Replacement.Highlight = True For j = 0 To UBound(Split(StrFnd, "|")) .Text = Split(StrFnd, "|")(j) .Replacement.Text = "^&" .Execute Replace:=wdReplaceAll Next End With Options.DefaultHighlightColorIndex = HiLt Application.ScreenUpdating = True End Sub
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Tags for this Thread

Posting Permissions

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