Consulting

Results 1 to 8 of 8

Thread: Find shaded text in Word and reformat as strikethrough?

  1. #1

    Find shaded text in Word and reformat as strikethrough?

    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!

    Shaded text sample.docx

  2. #2
    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
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3

    THIS IS GREAT!

    Thank you so much, Graham! This is precisely what I needed. Have a wonderful day.

    Quote Originally Posted by gmayor View Post
    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

  4. #4
    Quote Originally Posted by glrothman View Post
    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

  5. #5
    Quote Originally Posted by glrothman View Post
    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

  6. #6
    Graham,

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

    Gordon

  7. #7
    VBAX Contributor
    Joined
    Jul 2020
    Location
    Sun Prairie
    Posts
    123
    Location
    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

  8. #8
    Quote Originally Posted by Chas Kenyon View Post
    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.
    Last edited by Aussiebear; 02-26-2022 at 04:28 PM. Reason: Edited quote tags

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •