Page 1 of 1

Macro to delete highlighted text

PostPosted: Wed Jan 09, 2019 10:20 pm
by Baron Fou
Good afternoon!

I would like to find a solution to delete all highlighted text in yellow from a Word document. The reason is that at my job, I often need to delete the yellow text and when the document is 100 pages long, well it takes me a lot of time to do it by hand.

Do you know if a macro could do the work? I can easily find macros to change the color of the highlights (yellow to green for example), but I can't find anything to delete the highlighted text.

Please note there are often green, blue, pink... highlights in my documents - I only need the yellow deleted.

Thank you in advance!

Re: Macro to delete highlighted text

PostPosted: Wed Jan 09, 2019 10:27 pm
by RoryOF
You don't need a macro to do this.

Use Find and Replace. In Find click More Options, select Attributes and click the Character background box. Then click the Format button, click on the Background tab and select your desired colour. Now press Find All button, then hit Delete key.

I suggest you try with a small test file with several colour highlightings.

 Edit: Note also that you must choose the exact colour - there are, for example many shades of yellow, : Yellow, Yellow1,... Yellow10 

Re: Macro to delete highlighted text

PostPosted: Wed Jan 09, 2019 10:47 pm
by JeJe
As you've said Word and have Office 365 below it you could be in the wrong place? This forum is for OpenOffice... a search for Microsoft Word forum will find places to ask Word questions...

Re: Macro to delete highlighted text

PostPosted: Thu Jan 10, 2019 10:20 pm
by Baron Fou
Thank you RoryOF. I tried your solution, but I can't find the Character background box. It's probably another term in Word.

Jeje, you are right, I'm sorry! I didn't realize I was in an OpenOffice forum. I'll redirect my question ;)

Thanks to both of you!

Re: Macro to delete highlighted text

PostPosted: Fri Jan 11, 2019 12:33 am
by JeJe

Re: Macro to delete highlighted text

PostPosted: Fri Jan 11, 2019 12:42 am
by JeJe
Modify the sub there with objRange.Text = "" instead of the replacement color

Code: Select all   Expand viewCollapse view
  With Selection
    .HomeKey Unit:=wdStory
    With Selection.Find
      .Highlight = True

      Do While .Execute
        If Selection.Range.HighlightColorIndex = wdYellow Then
          Set objRange = Selection.Range
          objRange.Text = ""
          Selection.Collapse wdCollapseEnd
        End If
      Loop
    End With
  End With


Re: Macro to delete highlighted text

PostPosted: Fri Jan 11, 2019 12:51 am
by JeJe
Code: Select all   Expand viewCollapse view

sub test
DeleteHighlightColor wdYellow
end sub

Sub DeleteHighlightColor(strFindColor)
  Dim objDoc As Document
  Dim objRange As Range

  Application.ScreenUpdating = False

  Set objDoc = ActiveDocument

  With Selection
    .HomeKey Unit:=wdStory
    With Selection.Find
      .Highlight = True
      Do While .Execute
        If Selection.Range.HighlightColorIndex = strFindColor Then
          Set objRange = Selection.Range
          objRange.Text = ""
        End If
      Loop
    End With
  End With

  Application.ScreenUpdating = True
End Sub




Edit:

Its interesting that this appears to be one thing you can do in Writer but not in Word. I just have an old copy of Word and it appears you can delete all the highlighted text using the Find/Replace box but not individual colors.

What you could do to make it possible is to use character styles - so you define a style named yellowHighlight with that color of highlight and apply that style to the text instead of the highlight. Then you can search for/find that style instead of the highlight directly.

Edit 2
Nope that doesn't seem possible either - you can't define a character style with a highlight color in my old copy of Word - the text color and the underline color but not the highlight!