none
How to find "Shading" in Word document using color number reciving from WordOpenXML RRS feed

  • Question

  • I have to change every shading BackgroundPatternColor to Highlight. Changing character by character is very, very slow. Fastest way is using .Find method.

    But in documents can be a lot of different shading colors (24-bit color). In WordOpenXML is sth. like this:

    <w:shd w:val="clear" w:color="auto" w:fill="8064A2">

    or

    <w:shd w:val="clear" w:color="auto" w:fill="8064A2" w:themeFill="accent4"/>

    From this xml, I know that "8064A2" shading color was used in document - that "8064A4" number is hex RRGGBB mask , and its mean RGB(128, 100, 162) - hex "80" is 128 decimal etc.

    But... function RGB(128, 100, 162) return 10642560. The shading I'm looking for return -687800321

    Debug.print Selection.Range.Shading.BackgroundPatternColor

    So, I think that accent4 do something, but what?

    How to receive -687800321 number from RGB (because Selection.Find.Shading.BackgroundPatternColor = -687800321 works great)


    • Edited by fordata.dk Thursday, November 9, 2017 8:59 AM
    Thursday, November 9, 2017 8:58 AM

All replies

  • You could use something like:

    Sub Test()
    MsgBox GetRGB(Selection.Shading.BackgroundPatternColor)
    End Sub
    
    Function GetRGB(RGBvalue As Long) As String
    Dim StrTmp As String
    If RGBvalue < 0 Or RGBvalue > 16777215 Then RGBvalue = 0
    StrTmp = StrTmp & " R: " & RGBvalue \ 256 ^ 0 Mod 256
    StrTmp = StrTmp & " G: " & RGBvalue \ 256 ^ 1 Mod 256
    StrTmp = StrTmp & " B: " & RGBvalue \ 256 ^ 2 Mod 256
    GetRGB = StrTmp
    End Function


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thursday, November 9, 2017 9:10 AM
  • Maybe I coudn't explain my problem.

    I know R, G and B values from WordOpenXML.
    I have docs with a lots of different shadings.

    In WordOpenXML behind, shading can be defined as:

    <w:shd w:val="clear" w:color="auto" w:fill="8064A2">

    or as

    <w:shd w:val="clear" w:color="auto" w:fill="8064A2" w:themeFill="accent4"/>

    both of them have in XML the same RGB definition: "8064A2", but only one return correct RGB number when I use: Debug.print Selection.Range.Shading.BackgroundPatternColor (first one: "8064A2"==10642560)


    My problem is: how to get from w:fill="8064A2" w:themeFill="accent4" this value: -687800321 ?


    • Edited by fordata.dk Thursday, November 9, 2017 11:20 AM
    Thursday, November 9, 2017 11:14 AM
  • Whatever else it is, -687800321 is not an RGB value. RGB values can only be between 0 and 16777215; negative numbers are invalid and, even if you use it as a +ve value, that too is greater than 16777215. FWIW, 8064A2 = 8414370, = R:162, G:100, B:128, which you could get by calling the function I posted with:

    Sub Test()
    MsgBox "8064A2 = " & &H8064A2 & " = " & GetRGB(&H8064A2)
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thursday, November 9, 2017 10:10 PM
  • Thanks Poul that you waste your time for me but check this case:
    - Select text (I spoke about selection text - not table, cell)
    - Click like one the screen:

    In immediate window type this (still on selection)
    ?Selection.Range.Shading.BackgroundPatternColor

    I have value less then zero.
    In each color in this row of colors I have negative numbers.

    PS. Funny thing is when you click "More colors" and without changing anything in "Colors" window click OK. There will be positive (and correct RGB) value in BackgroundPatternColor.

    • Edited by fordata.dk Friday, November 10, 2017 10:11 AM
    Friday, November 10, 2017 10:08 AM
  • The theme value (-687800321) is not an RGB value. Using:
    ?Hex$(Selection.Range.Shading.BackgroundPatternColor)
    in the immediate window returns D700FFFF, not the 8064A2 you saw in the xml code.

    As you saw, the RGB values for the theme colour are given as R:128, G:100, B:162, but 8064A2 is equivalent to R:162, G:100, B:128 - the R & B values are swapped! If you apply the R:162, G:100, B:128 shading using the 'more colors' option, you'll get a diffierent colour. If you then then use:
    ?Hex$(Selection.Range.Shading.BackgroundPatternColor)
    in the immediate window, you'll get 8064A2, confirming you're now seeing the RGB values stored in the xml code.

    And just to make things really interesting, if you apply the theme's RGB colours via the 'more colors' option instead of choosing the 'purple, accent 4' box, then use:
    ?Hex$(Selection.Range.Shading.BackgroundPatternColor)
    in the immediate window, you'll get A26480, not the D700FFFF you get when using the theme - and, the 'purple, accent 4' box doesn't get re-selected if you go back into the shading popup. Clearly, the theme is doing more than simply applying a particular RGB value.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Friday, November 10, 2017 12:15 PM
  • "Clearly, the theme is doing more than simply applying a particular RGB value."

    Yes. Thanks for understanding, and this statement causes that my question is still up to date:
    How to find shading using color number reciving from WordOpenXML?

    I found accent4 value here:
    https://www.ecma-international.org/publications/standards/Ecma-376.htm 
    (Ecma Open Office XML Part 1 - topic 18.8.3)

    clrScheme has values:

    <clrScheme name="sample">
    <dk1><sysClr val="windowText"/></dk1>
    <lt1><sysClr val="window"/></lt1>
    <dk2><srgbClr val="04617B"/></dk2>
    <lt2><srgbClr val="DBF5F9"/></lt2>
    <accent1><srgbClr val="0F6FC6"/></accent1>
    <accent2><srgbClr val="009DD9"/></accent2>
    <accent3><srgbClr val="0BD0D9"/></accent3>
    <accent4><srgbClr val="10CF9B"/></accent4>
    <accent5><srgbClr val="7CCA62"/></accent5>
    <accent6><srgbClr val="A5C249"/></accent6>
    <hlink><srgbClr val="FF9800"/></hlink>20. DrawingML - Framework Reference Material 2841
    <folHlink><srgbClr val="F45511"/></folHlink>
    </clrScheme> 


    So... Any idea how to get -687800321 "mixing" 8064A2 and 7CCA62 values?



    edit_____
    I found and now try understand this:
    http://www.wordarticles.com/Articles/Colours/2007.php
    http://www.wordarticles.com/Articles/Colours/2007BuildSet.php


    • Edited by fordata.dk Tuesday, November 14, 2017 2:26 PM
    Tuesday, November 14, 2017 2:06 PM
  • Hi fordata,

    If you have any update about your issue, please feel free to let us know.

    Regards,

    Tony


    Help each other

    Friday, November 17, 2017 8:30 AM
  • Hi fordata.dk,

    All color in word is 32-bit value. When we try to set the color using a 24-bit values, the highest 8-bits is set to zero.

    However, for an accent color, the color's 24-bit is fixed as 00000000 11111111 11111111 and the first 8-bit values will be set according to accent color set.

    In the 8-bit values, first 4-bits is also fixed(1101) and the second 4-bit values will be set value as the value of WdThemeColorIndex Enumeration

    For instance, for Accent color 4, the value is 7(0111), its whole 32bits value is

    11010111 00000000 11111111 11111111=-687800321

    If we change the color to Accent 1, the value is 4(0100), its whole 32 bits value is 

    11010100 00000000 11111111 11111111=-738131969 

    It does not matter what's color of 8064A2 or 7CCA62. If we try to change color of Accent 4, such as 

    Now, if we try to return the color of a yellow color, since it is Text color 1 now so its value is 13. The whole 32-bit value should be

    1101 1101 00000000 11111111 11111111=-587137025 

    And for the black color, since it is Accent Color 4 now, so its 32-bit value should be

    1101 0111 00000000 11111111 11111111=-687800321

    Best Regards,

    Terry


    MSDN Community Support Please remember to click &amp;quot;Mark as Answer&amp;quot; the responses that resolved your issue, and to click &amp;quot;Unmark as Answer&amp;quot; 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, November 20, 2017 7:56 AM
  • Thanks for explaining accent influence to preparing colors, but...
    But Word does sth else. In xml behind the scene there are also themeFillTint and themeFillShade. They've influence in final color value (for last 16-bits)

    I've made test, for all this violet colors in screen in my previous post:
    In xml behind:
    <w:shd w:val="clear" w:color="auto" w:fill="8064A2" w:themeFill="accent4"/>
    1101011100000000 11111111 11111111 - binary value of Selection.Range.Color

    <w:shd w:val="clear" w:color="auto" w:fill="E5DFEC" w:themeFill="accent4" w:themeFillTint="33"/>
    1101011100000000 11111111 00110011

    <w:shd w:val="clear" w:color="auto" w:fill="CCC0D9" w:themeFill="accent4" w:themeFillTint="66"/>
    1101011100000000 11111111 01100110

    <w:shd w:val="clear" w:color="auto" w:fill="B2A1C7" w:themeFill="accent4" w:themeFillTint="99"/>
    1101011100000000 11111111 10011001

    <w:shd w:val="clear" w:color="auto" w:fill="5F497A" w:themeFill="accent4" w:themeFillShade="BF"/>
    1101011100000000 10111111 11111111

    <w:shd w:val="clear" w:color="auto" w:fill="403152" w:themeFill="accent4" w:themeFillShade="80"/>
    1101011100000000 10000000 11111111

    themeFillTint looks like last 8-bits
    hex(33)==00110011

    themeFillShade looks like 17-24bits
    hex(BF)==10111111

    Could you confirm my supposition?

    Are themeFillShade and themeFillShade mutually exlusive? I don't think so, but maybe someone know.

    And what about 9-16bits. What are responsible for?

    I feel I'm very close to understand this topic :-)


    • Edited by fordata.dk Monday, December 11, 2017 11:49 AM
    Friday, December 8, 2017 3:06 PM