Consulting

Results 1 to 6 of 6

Thread: How to highlight text between two finds

  1. #1
    VBAX Newbie
    Joined
    Jan 2023
    Posts
    5
    Location

    How to highlight text between two finds

    Hello,
    I have a MS Word document that has a lot of "notes to self" in it. The notes are captured in-between "[" and "]" characters.
    How can I create a macro that goes through the entire Word document and highlights all of the text between each pair of [] characters?

    thank you.

  2. #2
    VBAX Newbie
    Joined
    Jan 2023
    Posts
    5
    Location
    Here's a start (below). I'm not too good on Word, so I have not been able to figure out how to keep the attached macro running until it reaches the end of the document. You have to keep running it. Perhaps someone else will help out.

    Sub Highlighting()
    '' Highlighting Macro
    Selection.Find.ClearFormatting
        With Selection.Find
            .Text = "["
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
         End With
         Selection.Find.Execute
         i = 0
        Do While i < 200 'allows 200 characters
        i = i + 1
        Selection.Find.ClearFormatting
        With Selection.Find
            .Text = "?"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchAllWordForms = False
            .MatchSoundsLike = False
            .MatchWildcards = True
        End With
        Selection.Find.Execute
        Selection.Find.ClearFormatting
        With Selection.Find
            .Text = "?"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = True
            .MatchCase = False
            .MatchWholeWord = False
            .MatchAllWordForms = False
            .MatchSoundsLike = False
            .MatchWildcards = True
        End With
        If (Selection.Text) = "]" Then Exit Sub
            Options.DefaultHighlightColorIndex = wdYellow
            Selection.Range.HighlightColorIndex = wdYellow
        Loop
    End Sub

  3. #3
    VBAX Newbie
    Joined
    Jan 2023
    Posts
    5
    Location
    Thank you. It's been too long since I've written these things myself.
    I think there is a way to find "[" and "]" and select all of the text in between, but this does work for a single instance, so it's a start. TY

  4. #4
    VBAX Newbie
    Joined
    Jan 2023
    Posts
    5
    Location
    Further to my inelegant solution, you can get it to do all by adding the following code at the top, as shown, and change the If statement at the bottom, as shown. It will just keep doing the same thing until j runs out. If you can make j a little more than the number of instances of [], it will work.
    Sub Highlighting()
    '
    ' Highlighting Macro
    j = 0
    continue:
    j = j + 1
    If j = 5 Then Exit Sub
    .
    .
    .
     If (Selection.Text) = "]" Then GoTo continue

  5. #5
    VBAX Newbie
    Joined
    Jan 2023
    Posts
    5
    Location
    Thank you. It does work. BTW I added a "^" character at the end of my file and then added this line to the code to make it stop when it reaches the end of the file.

    If (Selection.Text) = "^" Then Exit Sub

    Thank you!

  6. #6
    VBAX Newbie
    Joined
    Jan 2023
    Posts
    5
    Location
    Great idea!

Tags for this Thread

Posting Permissions

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