Macro to delete highlighted text

Creating a macro - Writing a Script - Using the API

Macro to delete highlighted text

Postby Baron Fou » Wed Jan 09, 2019 10:20 pm

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
Baron Fou
 
Posts: 2
Joined: Wed Jan 09, 2019 10:10 pm

Re: Macro to delete highlighted text

Postby RoryOF » Wed Jan 09, 2019 10:27 pm

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.6 on Xubuntu 18.04.2 (mostly 64 bit version) and very infrequently on Win2K/XP
User avatar
RoryOF
Moderator
 
Posts: 28755
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Macro to delete highlighted text

Postby JeJe » Wed Jan 09, 2019 10:47 pm

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...
Openoffice 4.1.2
Windows 8
JeJe
Volunteer
 
Posts: 481
Joined: Wed Mar 09, 2016 2:40 pm

Re: Macro to delete highlighted text

Postby Baron Fou » Thu Jan 10, 2019 10:20 pm

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
Baron Fou
 
Posts: 2
Joined: Wed Jan 09, 2019 10:10 pm

Re: Macro to delete highlighted text

Postby JeJe » Fri Jan 11, 2019 12:33 am

Openoffice 4.1.2
Windows 8
JeJe
Volunteer
 
Posts: 481
Joined: Wed Mar 09, 2016 2:40 pm

Re: Macro to delete highlighted text

Postby JeJe » Fri Jan 11, 2019 12:42 am

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

Openoffice 4.1.2
Windows 8
JeJe
Volunteer
 
Posts: 481
Joined: Wed Mar 09, 2016 2:40 pm

Re: Macro to delete highlighted text

Postby JeJe » Fri Jan 11, 2019 12:51 am

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!
Openoffice 4.1.2
Windows 8
JeJe
Volunteer
 
Posts: 481
Joined: Wed Mar 09, 2016 2:40 pm

Re: Macro to delete highlighted text

Postby lumusislight » Fri Feb 01, 2019 1:42 pm



Hi JeJe,

I've been just searching for that and found this talk. Sometimes, there's a lucky chance to find what you need in a place you've never thought about.
Here's another helpful article (from Datanumen) in case anyone else is looking for a way to remove the highlight color - https://www.datanumen.com/blogs/remove- ... -document/
OpenOffice 4.1.2 on Windows 10
lumusislight
 
Posts: 2
Joined: Mon Oct 08, 2018 9:45 am


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 2 guests