PDA

View Full Version : [SOLVED:] Hightlight Keywords in a MS Word File



kashyap.dave
01-24-2020, 02:32 AM
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?

macropod
01-24-2020, 02:55 AM
What do you mean by "instance handle"? This isn't standard Word terminology.

kashyap.dave
01-24-2020, 03:28 AM
Hi Paul,

Sorry for the confusion.

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


Cheers!

gmayor
01-24-2020, 06:39 AM
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

macropod
01-24-2020, 02:27 PM
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