Consulting

Results 1 to 8 of 8

Thread: How to highlight text between two finds

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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!

  7. #7
    Quote Originally Posted by DanMan View Post
    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.
    A beginning is provided below. My Word skills are lacking, thus I haven't figured out how to make the attached macro run all the way to the document's conclusion. It must be kept functioning. Maybe someone else will pitch in as well.

  8. #8
    Moderator VBAX Guru Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    4,997
    Location
    Maybe try this on a copy of your document

    Sub FindSquareBracketPairs()
    Dim rngFind As Word.Range
    Dim sOpen As String, sClose As String
    Dim sFindTerm As String
    Dim bFound As Boolean, lPosOpen As Long
    Set rngFind = ActiveDocument.content
    sOpen = "["
    sClose = "]"
    sFindTerm = "\[*\]"
    With rngFind.Find
         .ClearFormatting
         .text = "\[*\]"
         .Forward = True
         .wrap = Word.WdFindWrap.wdFindStop
         .MatchWildcards = True
         bFound = .Execute
         Do While bFound
              lPosOpen = NumberOfCharInRange(rngFind, sOpen)
              rngFind.HighlightColorIndex = Word.WdColorIndex.wdYellow
              rngFind.Collapse wdCollapseEnd
              bFound = .Execute
         Loop
    End With
    End Sub
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

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
  •