开发者

Change text font color in Word document

开发者 https://www.devze.com 2023-02-16 08:54 出处:网络
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:

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开发者_JS百科 no Setter (ReadOnly).


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消