PDA

View Full Version : Find shaded text in Word and reformat as strikethrough?



glrothman
01-04-2022, 06:56 PM
Hello, and thanks in advance for any guidance you can give.

I have a stack of docx files, and each has sections with a shaded background, colored as follows: Pattern: Clear (Custom Color(RGB(232,198,201)))

I would love to have a macro that will find each of those sections and format them as strikethrough text (preferably with the shading replaced by a white background, but that is NOT essential).

Word/Microsoft 365/Windows 10

What I've tried: I looked for ways to find the target text with the Find/Replace tool. I created a Style based on the shaded text, then tried to search for that style. But every time, I got a "No Results Found" message.

I'm attaching an excerpt for a document with several examples of this shading.

If you need any additional info, just let me know. Thanks again!

29279

gmayor
01-04-2022, 11:04 PM
You cannot search for a style that you haven't applied to the text. However you can search for the background shading e.g.

Sub ReplaceShading()
'Graham Mayor - https://www.gmayor.com - Last updated - 05 Jan 2022
Dim oRng As Range
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Font.Shading.BackgroundPatternColor = RGB(232, 198, 201)
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
Do While .Execute
oRng.Font.StrikeThrough = True
oRng.Font.Shading.BackgroundPatternColor = wdColorAutomatic
oRng.Collapse 0
Loop
End With
Set oRng = Nothing
End Sub

glrothman
01-05-2022, 04:49 AM
Thank you so much, Graham! This is precisely what I needed. Have a wonderful day.


You cannot search for a style that you haven't applied to the text. However you can search for the background shading e.g.

Sub ReplaceShading()
'Graham Mayor - https://www.gmayor.com - Last updated - 05 Jan 2022
Dim oRng As Range
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Font.Shading.BackgroundPatternColor = RGB(232, 198, 201)
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
Do While .Execute
oRng.Font.StrikeThrough = True
oRng.Font.Shading.BackgroundPatternColor = wdColorAutomatic
oRng.Collapse 0
Loop
End With
Set oRng = Nothing
End Sub

glrothman
02-26-2022, 09:32 AM
Thank you so much, Graham! This is precisely what I needed. Have a wonderful day.

Graham,

The macro you wrote for me a few weeks ago was precisely what I asked for; changing a specific background shading to strikethrough. Could you please let me know what I'd need to change in order to add the strikethrough but LEAVE the shading?

Many thanks!!

Gordon

glrothman
02-26-2022, 11:10 AM
Graham,

The macro you wrote for me a few weeks ago was precisely what I asked for; changing a specific background shading to strikethrough. Could you please let me know what I'd need to change in order to add the strikethrough but LEAVE the shading?

Many thanks!!

Gordon

glrothman
02-26-2022, 11:10 AM
Graham,

I found the answer! I hope I haven't wasted any of your time since I posted. Take care...

Gordon

Chas Kenyon
02-26-2022, 12:35 PM
Just a note that you would be far ahead as far as automation and time saving if instead of direct formatting you were using styles.
Importance of Styles in Word (http://www.addbalance.com/usersguide/styles.htm#Overview)

glrothman
02-26-2022, 01:51 PM
Just a note that you would be far ahead as far as automation and time saving if instead of direct formatting you were using styles.

I don't disagree! Unfortunately, I'm working with documents that have been created by a website that - at least for now - is constrained to highlighting key sections with this very specific shading (and not even using a standard color). The oddest part is that when the user selects those key sections, it appears on-screen as strikethroughs. Only in the export process is it turned into shading.