Word文档中的文字更改字体颜色 [英] Change text font color in Word document
问题描述
我写了一个小的测试词插件,我无法找到一个方法来的更改字体颜色的字。
这里是我的代码:
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;
这不能编译,因为textColor属性没有二传手(只读)。
This won't compile because TextColor Property has no Setter (ReadOnly).
推荐答案
有两种方法可以做到这一点。您可以使用 Font.ColorIndex
为简单的选择或 Font.Fill.ForeColor
进行更广泛的选择。下面是一些VBA:
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
请注意。 Fill.ForeColor 之一,也有机会获得 RGB
属性,可以将字体设置为任何非稳定的色彩,如 s.Font.Fill.ForeColor.RGB = RGB(255,255,0)
将其设置为黄色。
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.
这篇关于Word文档中的文字更改字体颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!