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

C# 更改Word文档中的文本字体颜色,c#,windows,ms-word,C#,Windows,Ms Word,我写了一个小的测试单词插件,但我找不到方法来改变单词的字体颜色。 这是我的密码: var wordsList = this.Application.ActiveDocument.Words; wordsList[i].Font.TextColor = WdColor.wdColorRed; 这将无法编译,因为TextColor属性没有Setter(只读)。有两种方法。您可以使用Font.ColorIndex进行简单选择,也可以使用Font.Fill.ForeColor进行更广泛的选择。以下是一

我写了一个小的测试单词插件,但我找不到方法来改变单词的字体颜色。 这是我的密码:

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

这将无法编译,因为TextColor属性没有Setter(只读)。

有两种方法。您可以使用
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
one上,您还可以访问
RGB
属性,并可以将字体设置为任何非恒定颜色,如
s.Font.Fill.ForeColor.RGB=RGB(255,255,0)
将其设置为黄色。

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

下面的内容对您有用吗?如果是,您可以选择答案旁边的空心复选标记