Log in

View Full Version : [SOLVED:] Delete All Highlighted Words?



VB-AN-IZ
07-01-2017, 04:22 PM
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/showthread.php?57443-Highlight-All-Words-Containing-Capital-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!

gmayor
07-01-2017, 08:27 PM
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

VB-AN-IZ
07-12-2017, 11:00 AM
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.