更改Word文档中的文本字体颜色

时间:2011-03-13 20:05:55

标签: c# windows ms-word

我写了一个小测试单词addon,我找不到一种方法来更改单词的字体颜色。 这是我的代码:

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

这不会编译,因为TextColor属性没有Setter(ReadOnly)。

2 个答案:

答案 0 :(得分:8)

有两种方法可以做到这一点。您可以使用Font.ColorIndex进行简单选择,也可以使用Font.Fill.ForeColor进行更广泛的选择。这是一些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

关于Font.Fill.ForeColor的注释,您还可以访问RGB属性,并可以将字体设置为任何非常量颜色,例如s.Font.Fill.ForeColor.RGB = RGB(255, 255, 0)将其设置为黄色。

答案 1 :(得分:4)

您需要设置Font.ColorIndex = Word.WdColorIndex.wdRed,而不是TextColor属性。将索引设置为您需要的设置。

相关问题