Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change text font color in Word document

I wrote a small test word addon and I can't find a way to change the font color of a word. Here's my code:

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

This won't compile because TextColor Property has no Setter (ReadOnly).

like image 733
ghet Avatar asked Mar 13 '11 20:03

ghet


2 Answers

There are two ways to do it. You can either use Font.ColorIndex for simple choices or Font.Fill.ForeColor for more extensive choices. Here's some VBA:

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 the Font.Fill.ForeColor one, you also have access to the RGB property and can set the font to any non-constant color, like s.Font.Fill.ForeColor.RGB = RGB(255, 255, 0) sets it to yellow.

like image 133
Todd Main Avatar answered Oct 18 '22 03:10

Todd Main


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

like image 38
Richard Ciner Avatar answered Oct 18 '22 03:10

Richard Ciner