Macro to delete highlighted text

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Baron Fou
Posts: 2
Joined: Wed Jan 09, 2019 10:10 pm

Macro to delete highlighted text

Post 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!
Office 365 Windows 10
User avatar
RoryOF
Moderator
Posts: 34611
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Macro to delete highlighted text

Post 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 
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: Macro to delete highlighted text

Post 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...
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Baron Fou
Posts: 2
Joined: Wed Jan 09, 2019 10:10 pm

Re: Macro to delete highlighted text

Post 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!
Office 365 Windows 10
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: Macro to delete highlighted text

Post by JeJe »

Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: Macro to delete highlighted text

Post by JeJe »

Modify the sub there with objRange.Text = "" instead of the replacement color

Code: Select all

  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

Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: Macro to delete highlighted text

Post by JeJe »

Code: Select all


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!
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Post Reply