2011-03-13 83 views
4

我写了一个小测试词插件,我无法找到一种方法来更改字体颜色的一个词。 这里是我的代码:更改Word文档中的文本字体颜色

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

这不能编译,因为textColor属性没有二传手(只读)。

+0

为您做了以下工作吗?如果是这样,您可以选择答案旁边的空白复选标记 –

回答

6

有两种方法可以做到这一点。您可以使用Font.ColorIndex进行简单选择,或者使用Font.Fill.ForeColor进行更广泛的选择。下面是一些VBA:在Font.Fill.ForeColor一个

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 

请注意,您也可以访问RGB财产,可以将字体设置为任何非恒定的颜色,像s.Font.Fill.ForeColor.RGB = RGB(255, 255, 0)将其设置为黄色。

4

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