Change the font color of text in a Word document

I wrote a little test word addon, and I cannot find a way to change the font color of the word. Here is my code:

var wordsList = this.Application.ActiveDocument.Words; wordsList[i].Font.TextColor = WdColor.wdColorRed; 

This will not compile because the TextColor property does not have a Setter (ReadOnly).

+4
source share
2 answers

There are two ways to do this. You can use Font.ColorIndex for simple choices or Font.Fill.ForeColor for more choices. Here are some VBAs:

 Sub ChangeColorThisWay() Dim s As Range: Set s = Selection.Range s.Font.Fill.ForeColor = WdColor.wdColorRed End Sub Sub ChangeColorThatWay() Dim s As Range: Set s = Selection.Range s.Font.ColorIndex = WdColorIndex.wdBrightGreen End Sub 

Note on Font.Fill.ForeColor , you also have access to the RGB property and can set the font to any inconsistent color, for example, s.Font.Fill.ForeColor.RGB = RGB(255, 255, 0) sets it to yellow.

+6
source

You need to set Font.ColorIndex = Word.WdColorIndex.wdRed , not the TextColor property. Set the index in what you need and you set.

+4
source

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


All Articles