Results 1 to 4 of 4
  1. #1
    2 Star Lounger
    Join Date
    Sep 2005
    Location
    Poughkeepsie, New York, USA
    Posts
    123
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Word Macro to Highlight Text (Word 2003 SP2)

    <img src=/S/help.gif border=0 alt=help width=23 height=15> 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 ([img]/forums/images/smilies/cool.gif[/img] 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

  2. #2
    2 Star Lounger
    Join Date
    Sep 2005
    Location
    Poughkeepsie, New York, USA
    Posts
    123
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word Macro to Highlight Text (Word 2003 SP2)

    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. <img src=/S/sigh.gif border=0 alt=sigh width=15 height=15> 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. <img src=/S/thankyou.gif border=0 alt=thankyou width=40 height=15>

    Rich

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Word Macro to Highlight Text (Word 2003 SP2)

    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.

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Word Macro to Highlight Text (Word 2003 SP2)

    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

Posting Permissions

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