none
Macro cannot find shading colour black RRS feed

  • Question

  • I have a macro which should search & delete all paragraphs with black shading that works pretty well, except that it is unable to distinguish/find text with black shading colour. Here is the part of the macro that performs the search:

    Selection.Find.ClearFormatting
        With Selection.Find.ParagraphFormat
            .Shading.BackgroundPatternColor = wdColorBlack
        End With
        With Selection.Find
        .Wrap = wdFindContinue
        End With
        Selection.Find.Execute

    If 'wdColorBlack' is substituted with yellow or red, the respective paragraphs are  effortlessly found by the macro; if 'wdColorBlack' or '0' or '&H0' are specified, paragraphs with no shading are found by the macro along with paragraphs in black shading. 

    Some articles on the colours in MS Word indicate that certain codes for theme or colour accent are added to the final colour code, yet in the XML code of the document the text with black shading has no theme or colour accent modifiers. This is the corresponding excerpt from the .docx' XML code (the shading tag is marked up):

    [CODE]<w:p w:rsidR="00FE728A" w:rsidRPr="00FE728A" w:rsidRDefault="00FE728A" w:rsidP="00FE728A"><w:pPr><w:shd w:val="clear" w:color="auto" w:fill="000000"/><w:spacing w:after="0" w:line="570" w:lineRule="atLeast"/><w:rPr><w:rFonts w:ascii="scala-sans-sc-offc-pro--" w:eastAsia="Times New Roman" w:hAnsi="scala-sans-sc-offc-pro--" w:cs="Times New Roman"/><w:b/><w:bCs/><w:sz w:val="24"/><w:szCs w:val="24"/><w:lang w:val="en" w:eastAsia="en-GB"/></w:rPr></w:pPr><w:r w:rsidRPr="00FE728A"><w:rPr><w:rFonts w:ascii="scala-sans-sc-offc-pro--" w:eastAsia="Times New Roman" w:hAnsi="scala-sans-sc-offc-pro--" w:cs="Times New Roman"/><w:b/><w:bCs/><w:sz w:val="24"/><w:szCs w:val="24"/><w:lang w:val="en" w:eastAsia="en-GB"/></w:rPr><w:t xml:space="preserve">WORDS IN BLACK SHADING</w:t></w:r></w:p>
    [/CODE]

    The XML code of the text without shading:

    [CODE]<w:p w:rsidR="000140C9" w:rsidRDefault="000140C9" w:rsidP="000140C9"><w:r><w:t xml:space="preserve">TEXT WITHOUT SHADING</w:t></w:r></w:p>
    [/CODE]

    XML code of yellow shading:

    [CODE]<w:p w:rsidR="000140C9" w:rsidRDefault="000140C9" w:rsidP="000140C9"><w:pPr><w:shd w:val="clear" w:color="auto" w:fill="FF0000"/></w:pPr><w:r><w:t xml:space="preserve">TEXT IN YELLOW SHADING</w:t></w:r></w:p>[/CODE]

    How should the search parameter .Shading.BackgroundPatternColor be specified so that it finds the text with black shading?

     
    • Edited by Aquinax Sunday, April 1, 2018 5:42 PM
    Sunday, April 1, 2018 2:52 PM

All replies

  • Cross-posted at: http://www.vbaexpress.com/forum/showthread.php?62402-Macro-cannot-find-shading-colour-black
    For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Sunday, April 1, 2018 9:01 PM
  • Hello Aquinax,

    >>If 'wdColorBlack' is substituted with yellow or red, the respective paragraphs are  effortlessly found by the macro

    I failed to find respective paragraphs using yellow or red with your code. In my test, below code could work for me. And based on the code, wdColorBlack works too.

    Here is the code and demonstration.

    Sub FindTest()
        Selection.Find.ClearFormatting
        With Selection.Find.Font
            .Shading.BackgroundPatternColor = wdColorBlack
        End With
        With Selection.Find
        .Wrap = wdFindContinue
        End With
        Selection.Find.Execute
    End Sub

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, April 2, 2018 8:51 AM
  • The macro is supposed to find and delete paragraphs, hence the search for ParagraphFormat attrubutes - not Font's -and the macro itself is this:

    Sub FIND_AND_DEL_BLACK()
    
       Selection.Find.ClearFormatting
       With Selection.Find.ParagraphFormat
          .Shading.BackgroundPatternColor = &HD05068
       End With
       With Selection.Find
       .Wrap = wdFindContinue
       End With
       While Selection.Find.Execute = True
       Selection.Find.Execute
       Selection.Delete
       Wend
    
    End Sub

    As indicated in the linked articles, the complete colour code is 32 bit - not 24 bit - and the additional 2 HEX-characters change the resultant final colour code in INT or HEX ... thus MSWord cannot find these 'black shaded' paragraphs. I believe this is a question about the correct colour code representation in MSWord, the macro finds and deletes par-s with the colour code correctly specifed successfully ... 

    Monday, April 2, 2018 2:25 PM
  • Hello Aquinax,

    How do you set the shading background color? Could my code work for your? Could you share a simply document so we could use it to reproduce your issue?

    For sharing file, you could share the file via Cloud Storage, such as One Drive, and then put link address here. Thanks for understanding.

    >>As indicated in the linked articles

    Where is the linked articles?

    >>the complete colour code is 32 bit - not 24 bit - and the additional 2 HEX-characters change the resultant final colour code in INT or HEX

    As far as I know, the highest 8-bits are usually fixed except the color is a theme color. And I think the shading background color is not theme color. So I think it will not be the reason you think why MSWord cannot find these paragraphs.

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, April 3, 2018 6:07 AM