none
Finding color of a Range RRS feed

  • Question

  • I'm developing an Word add-in. What is the best way to find the color of the text in a Range object in RGB format? 

    I've tried using Range.Font.ColorIndex, but it gives an enum and I don't know how to convert it to RGB.

    I also tried Range.Font.TextColor, but it throws an exception saying its unimplemented.

    Friday, July 6, 2012 7:16 AM

Answers

  • Hi Rishi

    I take it this is Word 2010?

    Prior to this version, the Font object had a Color property. Actually, it still has the property, it's just a "hidden member", meaning it's in the process of being replaced by something else (probably TextColor). All these things remain part of the object for compatibility reasons, but their use is discouraged by making them invisible.

    The property returns an Integer that represents the RGB color, for example 52479 for R=255 G=204 B=0.

    There is a way to convert this information to RGB, but I'm not a color-oriented person... But see

    http://www.wordbanter.com/showthread.php?t=35069

    I tried some quick tests and

    Hex(Range.Font.Color) does return a hexidecimal value for the color (#00CCFF for the above). If I plug this into the color converter at http://www.web-wise-wizard.com/html-tutorials/hex-rgb-web-color-converter.html, however, the color I get is incorrect. I have to reverse the color pairs (enter FFCC00 instead). The explanation for this appears to be http://stackoverflow.com/questions/9967534/vsto-font-color-returns-confusing-values.

    I hope this is enough to get you going, but if not, we can move the question to the Word for Developers forum, where you'll find more Word-oriented people than frequent the VSTO forum.


    Cindy Meister, VSTO/Word MVP

    Friday, July 6, 2012 8:38 AM
    Moderator

All replies

  • Hi Rishi

    I take it this is Word 2010?

    Prior to this version, the Font object had a Color property. Actually, it still has the property, it's just a "hidden member", meaning it's in the process of being replaced by something else (probably TextColor). All these things remain part of the object for compatibility reasons, but their use is discouraged by making them invisible.

    The property returns an Integer that represents the RGB color, for example 52479 for R=255 G=204 B=0.

    There is a way to convert this information to RGB, but I'm not a color-oriented person... But see

    http://www.wordbanter.com/showthread.php?t=35069

    I tried some quick tests and

    Hex(Range.Font.Color) does return a hexidecimal value for the color (#00CCFF for the above). If I plug this into the color converter at http://www.web-wise-wizard.com/html-tutorials/hex-rgb-web-color-converter.html, however, the color I get is incorrect. I have to reverse the color pairs (enter FFCC00 instead). The explanation for this appears to be http://stackoverflow.com/questions/9967534/vsto-font-color-returns-confusing-values.

    I hope this is enough to get you going, but if not, we can move the question to the Word for Developers forum, where you'll find more Word-oriented people than frequent the VSTO forum.


    Cindy Meister, VSTO/Word MVP

    Friday, July 6, 2012 8:38 AM
    Moderator
  • Hi,

    Yeah, I'm using Word 2010 and so, Color property is invisible. TextColor seems like a replacement, but when I use it the following exception is thrown -"...threw an exception of type 'System.NotImplementedException' Microsoft.Office.Interop.Word.ColorFormat {System.NotImplementedException}"

    I'm new to C# (started learning it only for Word add-in development) and I can't understand what this means.

    • Edited by RishiK Friday, July 6, 2012 9:03 AM
    Friday, July 6, 2012 9:01 AM
  • Hi Rishi

    It means that TextColor isn't available in 2010. Will C# let you use Color, even though it's not in Intellisense?


    Cindy Meister, VSTO/Word MVP

    Friday, July 6, 2012 9:22 AM
    Moderator
  • Hi,

    I'm able to use Color, but Color.RGB does not exist. I tried to use by casting Color to int, but that gives me a negative value.

    Friday, July 6, 2012 10:45 AM
  • And have you read the links I gave you? Those explain how to convert the value you get to RGB...

    Cindy Meister, VSTO/Word MVP

    Friday, July 6, 2012 9:53 PM
    Moderator
  • Yeah, I read them and performed the same. But the values given are confusing. They don't map to RGB.
    Saturday, July 7, 2012 6:12 AM
  • As explained in Jay Freedman's posts, the value returned needs to be converted into a HEX value, which can then be parsed into the three RGB values. But the HEX value returned on most Windows machines will be in the order BGR.

    Cindy Meister, VSTO/Word MVP

    Monday, July 9, 2012 7:40 AM
    Moderator