Question

I want to set different colors for all series in my chart in Word 2010 through a windows service using VS 2012 and interop assemblies for word.

I know to set it like:

    Series series1.Format.Line.ForeColor.RGB = (int)XlRgbColor.xlDarkGoldenrod;

BUT I want to set exact RGB values e.g.(111,154,169) and not some predefined colors (not even hexValues).

I am finding some way to do this using System.Drawing.Color structure but in vain.

Was it helpful?

Solution

Try:

Series series1.Format.Line.ForeColor.RGB = System.Drawing.Color.FromArgb(111,154,169).ToArgb()

Note that at least in Excel the R and B values are sometimes reversed, so if the color won' match try to switch the R and B components.

See this quote from another thread: Changing an Excel cell's backcolor using hex results in Excel displaying completely different color in the spreadsheet

I finally figured it out, after lots of tests, and it was something really simple. Apparently, Excel's Interop library has a bug and is reversing the Red and Blue values, so instead of passing it a hex of RGB, I need to pass BGR, and suddenly the colors work just fine. I'm amazed that this bug isn't documented anywhere else on the internet.

So if anyone else ever runs into this problem, simply pass Excel values in BGR values. (Or if using Color.FromArgb(), pass in Color.FromArgb(B, G, R))

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top