6

the situation:

  • .net 3.5
  • c# or vb.net(also tested)
  • word 2007 add-in

I'm trying to set the Background Color of the text to a custom rgb color.

The code is the following:

Range r = this.Application.ActiveDocument.Range();
r.Text = "blabla";
r.Font.Shading.BackgroundPatternColor =(WdColor) Color.FromArgb(0, 214, 227,188).ToArgb();

At first it seems to work, except that the color is not the right one. It seems that whenever I set a custom color, it changes it to an existing WdColor constant. Having a look at the doc, it says :

Returns or sets the 24-bit color that's applied to the background of the Shading object. Can be any valid WdColor constant or a value returned by Visual Basic's RGB function.

So, my question is: does anybody has an idea of how it's supposed to work?

Thanx

lnu
  • 1,404
  • 1
  • 9
  • 25
  • Possible duplicate [change-text-font-color-in-word-document](http://stackoverflow.com/questions/5292007/change-text-font-color-in-word-document) – Marshal Apr 16 '12 at 13:56
  • It says basically the same thing as the doc: use and index or a custom one but this is what is not working. – lnu Apr 16 '12 at 13:59

1 Answers1

11

Use ColorTranslator

Range r = this.Application.ActiveDocument.Range();
r.Text = "blabla";
r.Font.Shading.BackgroundPatternColor =(WdColor)ColorTranslator.ToOle(0, 214, 227,188);
Kiru
  • 3,489
  • 1
  • 25
  • 46
  • 1
    The sample code does not work as given; I believe it is missing an intermediate method call: `ColorTranslator.ToOle(Color.FromArgb(0, 214, 227,188));`. Perhaps a different .Net version? – Chris Aug 23 '16 at 18:29