• Word Macro to Highlight Text (Word 2003 SP2)

    Home » Forums » AskWoody support » Productivity software by function » MS Word and word processing help » Word Macro to Highlight Text (Word 2003 SP2)

    Author
    Topic
    #426800

    help I have been trying to write a VBA macro (described below) myself for nearly a week and simply do not understand enough about programming to do so. I am hoping someone here will be willing to help me, either privately or here on the forum.

    Here is what I am trying to accomplish:

    1. When I come to a word or phrase in a document that I want to mark with highlight, I want to select the word and then run the macro.

    2. The macro should bring up a message box asking whether I want to highlight the term, Yes, Cancel.

    3. If I choose yes, the selected term is highlighted, the term is deselected by moving the cursor to the position immediately following the term, and the macro then (a) inserts a bookmark called markreturn and then ( searches the document forward only for the next instance of that exact term, including case match and any punctuation (the term could be a single word or a phrase, i.e., its character length and content can vary with each running of the macro).

    4. If it finds another instance of the term, a message box appears asking if I want to highlight the term, Yes, No, Cancel. Yes highlights the term and then causes the macro to search forward only for the next instance of the exact term; no bookmark is inserted. No skips this instance of the term and searches for the next instance, and if it finds the term, this step is repeated.

    5. This process continues until the end of the document is reached or no additional instance of the term is found or Cancel is chosen. In each of these instances, the macro returns to the markreturn bookmark and quits.

    6. When the macro quits, the Find term has to be cleared out so that the next time the macro runs (or the next time I do a search with another macro) there is no accumulation of terms.

    With some struggle, I was able to write a portion of the macro so that it highlights the selected term in the first instance, but I was unable to figure out how to get it to search again. However, that coding has morphed into something else that doesn’t really work and I (stupidly, I admit) didn’t save iterations of what I was doing. (Can you tell that I’m not much of a programmer?)

    Any help will be appreciated. Thank you for your time.

    Rich

    Viewing 0 reply threads
    Author
    Replies
    • #987242

      Edited by HansV to correct code

      Try this:

      Sub Hilite()
      Dim strText As String
      Dim intReturn As Integer

      ‘ Check that user has selected some text
      If Not Selection.Type = wdSelectionNormal Then
      MsgBox “Please select some text.”, vbInformation
      Exit Sub
      End If

      strText = Selection.Text
      ‘ First (selected) instance)
      Select Case MsgBox(“Do you want to highlight the text?”, _
      vbQuestion + vbYesNoCancel)
      Case vbYes
      Selection.Range.HighlightColorIndex = wdYellow
      Selection.Collapse Direction:=wdCollapseEnd
      ActiveDocument.Bookmarks.Add “MarkReturn”, Selection.Range
      Case vbCancel
      Exit Sub
      End Select

      ‘ Initialize Find
      With Selection.Find
      .Text = strText
      .Replacement.Text = “”
      .ClearFormatting
      .Replacement.ClearFormatting
      .Wrap = wdFindStop
      .MatchCase = True
      .MatchWholeWord = True
      ‘ Find following instances
      Do While .Execute
      Select Case MsgBox(“Do you want to highlight the text?”, _
      vbQuestion + vbYesNoCancel)
      Case vbYes
      Selection.Range.HighlightColorIndex = wdYellow
      Case vbCancel
      Exit Sub
      End Select
      Loop
      End With
      End Sub

      • #987249

        Thank you, Hans.

        The code you provided works (I only had to change the ? in the two Select Case MsgBox statements to _). After reading it, I can tell you that I was so far off in my coding that I shouldn’t even call what I had done coding. sigh With this core code, I think I can finish writing the code for the macro — I will give it a try. I appreciate the help and the quick response. thankyou

        Rich

        • #987250

          I have no idea why the underscores got changed to question marks between the VB editor and the post, I’m glad you found it yourself.

    Viewing 0 reply threads
    Reply To: Word Macro to Highlight Text (Word 2003 SP2)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: