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

6

我写了一个小的Word插件测试,但是我找不到一种可以更改单词字体颜色的方法。 以下是我的代码:

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

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


以下内容是否适用于您?如果是,请选择答案旁边的空心复选框。 - Todd Main
2个回答

7

有两种方法可以实现。你可以使用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)将其设置为黄色。


4

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接