Change text color in Word

situation:

  • .net 3.5
  • C # or vb.net (also verified)
  • Word 2007 add-in

I am trying to set the background color of the text to a custom rgb color.

The code is as follows:

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

This works first, except that the color is wrong. It seems that whenever I set a custom color, it changes it to an existing WdColor constant. Glancing at the doc , he says:

Gets or sets the 24-bit color that is applied to the background of the Shading object. It can be any valid WdColor constant or the value returned by the Visual Basic RGB function.

So my question is: does anyone have an idea of โ€‹โ€‹how it should work?

Thanx

+6
source share
1 answer

Use ColorTranslator

 Range r = this.Application.ActiveDocument.Range(); r.Text = "blabla"; r.Font.Shading.BackgroundPatternColor =(WdColor)ColorTranslator.ToOle(0, 214, 227,188); 
+11
source

Source: https://habr.com/ru/post/913326/


All Articles