PDA

View Full Version : Keyword Search Hit Color



R. Racoon
09-26-2023, 10:29 AM
I have the following simple keyword search sub that basically works for my purposes, however it highlights the hits in dull gray. How can I modify the code to change the (temporary) highlight color to something more visible (e.g. light pink)?


Sub Word_Find()
Dim FindText As String, Ask As Integer
Selection.HomeKey wdStory
FindText = InputBox("What would you like to search for?", "SEARCH FOR KEYWORD'")
If FindText = "" Then GoTo Stop1
Selection.Find.Execute FindText
Do Until Selection.Find.Found = False
Ask = MsgBox("Found - search for more?", vbYesNo, "SEARCH")
Selection.MoveRight
Selection.Find.Execute
Loop
Stop1:
MsgBox "No more found!"
If Ask = vbNo Then Exit Sub
End Sub

Vladimir
09-27-2023, 02:42 AM
Hi! Your code selects but not highlights the found keyword. To highlight it temporarily, try using the following:



Sub Word_Find()

Dim FindText As String, Ask As Integer
selection.HomeKey wdStory

FindText = InputBox("What would you like to search for?", "SEARCH FOR KEYWORD'")

If FindText = "" Then GoTo Stop1
selection.Find.Execute FindText
selection.range.HighlightColorIndex = wdPink
Do Until selection.Find.found = False
Ask = MsgBox("Found - search for more?", vbYesNo, "SEARCH")
If Ask = vbNo Then selection.range.HighlightColorIndex = wdNone: Exit Sub
selection.range.HighlightColorIndex = wdNone
selection.Find.Execute
selection.range.HighlightColorIndex = wdPink
Loop
Stop1:
MsgBox "No more found!"
End Sub

R. Racoon
09-27-2023, 09:38 AM
Vladimir,

Thanks for your feedback. It seems to want to work, but all of the hits still show up hightlighted in gray, except the last one which gets a "permanent" pink hightlight.

Vladimir
09-27-2023, 11:34 AM
It's strange, because I tested it (and I retested it again) and it works correctly. Could you please post your sample doc.

R. Racoon
09-27-2023, 12:57 PM
Vladimir,

I can't upload my original file (too much proprietary content), but I pasted the code into another one and I get the same result. For this file, I searched on "ethernet" and it found multiple hits, each highlighted in gray, until the last one which was highlighted in "permanent" pink, as you will see . . .

Are you maybe testing it on an Excel document? I posted in the Word Help forum.


Vladimir
09-27-2023, 01:52 PM
Ha-ha! It's weird! The code works perfectly on your file! I don't know the reason for this misbehavior. Maybe it's Word settings that are to blame. Maybe someone else will help us to figure it out.
R.Racoon, now the last hit won't remain selected:


Sub Hilite_Find()
Dim FindText As String, Ask As Integer
selection.HomeKey wdStory
FindText = InputBox("What would you like to search for?", "SEARCH FOR KEYWORD")
If FindText = "" Then GoTo exit_lbl
selection.Find.Execute FindText
selection.range.HighlightColorIndex = wdPink
Do Until selection.Find.found = False
Ask = MsgBox("Found - search for more?", vbYesNo, "SEARCH")
If Ask = vbNo Then selection.range.HighlightColorIndex = wdNone: Exit Sub
selection.range.HighlightColorIndex = wdNone
selection.Find.Execute
selection.range.HighlightColorIndex = wdPink
Loop
exit_lbl:
MsgBox "No more found!"
selection.range.HighlightColorIndex = wdNone
End Sub

R. Racoon
09-27-2023, 02:07 PM
Vladimir,

If I understand you correctly, the file I uploaded behaves correctly for you (but not for me). You're probably right, there must be a setting in my version of Word (Office Pro 2019) that is causing this.

Vladimir
09-27-2023, 02:20 PM
Mine is Office 2016.