Enhancements for Simple Keyword Search

  • I have this simple sub in MS Word to search for a keyword.

    It works fine, but the hits are highlighted in gray.

    Q1. What line of code is needed to change to a more standout color (e.g. lt. yellow)?

    Also, when a hit is found, the line containing the hit is always at the top of the screen.

    Q2. How can I make it show 2 or 3 lines above the line of the hit, and still keep the highlight intact?


    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("Keyword was 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

  • Something like ...?

  • Cytop,


    Thanks much. That works for my purposes.


    Sorry I haven't acknowledged your solution sooner. For some reason I'm not getting notifications of responses, even though I've enabled that otpion, so I wasn't aware of your until I decided to check on a whim . . .

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!