windows 更改 Word 文档中的文本字体颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5292007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change text font color in Word document
提问by ghet
I wrote a small test word addon and I can't find a way to change the font colorof a word. Here's my code:
我写了一个小的测试词插件,但我找不到改变一个词的字体颜色的方法。这是我的代码:
var wordsList = this.Application.ActiveDocument.Words;
wordsList[i].Font.TextColor = WdColor.wdColorRed;
This won't compile because TextColor Property has no Setter (ReadOnly).
这不会编译,因为 TextColor 属性没有 Setter (ReadOnly)。
回答by Todd Main
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:
有两种方法可以做到。您可以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
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.
请注意Font.Fill.ForeColor
,您还可以访问该RGB
属性,并且可以将字体设置为任何非常量的颜色,例如s.Font.Fill.ForeColor.RGB = RGB(255, 255, 0)
将其设置为黄色。
回答by Richard Ciner
You need to set Font.ColorIndex = Word.WdColorIndex.wdRed
, not the TextColor
property. Set the index to what you need and you are set.
您需要设置Font.ColorIndex = Word.WdColorIndex.wdRed
,而不是TextColor
属性。将索引设置为您需要的值,然后就设置好了。