Consulting

Results 1 to 3 of 3

Thread: Delete All Highlighted Words?

  1. #1

    Delete All Highlighted Words?

    How can I delete all highlighted words from a document? When recording a macro, it came up with this:

    Selection.Find.ClearFormatting    Selection.Find.Highlight = True
        With Selection.Find
            .Text = ""
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = True
    
    
        End With
        Selection.Delete Unit:=wdCharacter, Count:=1
    End Sub
    ...which only deleted one character.

    If it matters, the full idea here was to first highlight and extract any words containing capital letters, as previously solved:
    http://www.vbaexpress.com/forum/show...apital-Letters

    Sub ScratchMacro()      'A basic Word macro coded by Greg Maxey
        Dim oRng As Range 
        Set oRng = ActiveDocument.Range 
        With oRng.Find 
            Do While .Execute(FindText:="[A-Z]*>", MatchWildcards:=True) 
                oRng.HighlightColorIndex = wdYellow 
                oRng.Collapse 0 
            Loop 
        End With 
    lbl_Exit: 
        Exit Sub 
    End Sub
    Thanks!

  2. #2
    If you used Greg's macro to add the highlight then

    Sub Macro2()
    'Graham Mayor - http://www.gmayor.com - Last updated - 02 Jul 2017
    Dim oRng As Range
        Set oRng = ActiveDocument.Range
        With oRng.Find
            Do While .Execute(FindText:="[A-Z]*>", MatchWildcards:=True)
                If oRng.HighlightColorIndex = wdYellow Then
                    oRng.Text = ""
                End If
                oRng.Collapse 0
            Loop
        End With
    lbl_Exit:
        Exit Sub
    End Sub
    or to remove ALL (or only yellow) highlighted text

    Sub Macro3()
    'Graham Mayor - http://www.gmayor.com - Last updated - 02 Jul 2017
    Dim oRng As Range
        Set oRng = ActiveDocument.Range
        With oRng.Find
            .Highlight = True
            Do While .Execute(Forward:=True)
                If oRng.HighlightColorIndex = wdYellow Then 'Optionally remove only yellow highlighted text
                    oRng.Text = ""
                End If 'Optional with above
                oRng.Collapse 0
            Loop
        End With
    lbl_Exit:
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    Thanks very much!

    Ultimately I used the second of your options and removed the color-specific lines:

    If oRng.HighlightColorIndex = wdYellow Then
                End If
    But, good to know that's an option as well!

    Thanks again.

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
  •