Consulting

Results 1 to 8 of 8

Thread: Keyword Search Hit Color

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Keyword Search Hit Color

    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
    Last edited by Aussiebear; 09-27-2023 at 12:44 AM. Reason: Added code tags to supplied code

  2. #2
    VBAX Regular
    Joined
    Jan 2022
    Posts
    19
    Location
    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
    Last edited by Vladimir; 09-27-2023 at 03:03 AM.

  3. #3
    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.

  4. #4
    VBAX Regular
    Joined
    Jan 2022
    Posts
    19
    Location
    It's strange, because I tested it (and I retested it again) and it works correctly. Could you please post your sample doc.

  5. #5
    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.

    Attached Files Attached Files

  6. #6
    VBAX Regular
    Joined
    Jan 2022
    Posts
    19
    Location
    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
    Last edited by Aussiebear; 09-27-2023 at 04:24 PM. Reason: Wrapped supplied code with code tags

  7. #7
    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.

  8. #8
    VBAX Regular
    Joined
    Jan 2022
    Posts
    19
    Location
    Mine is Office 2016.

Posting Permissions

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