VBA Word:Font.TextColor 和 Font.ColorIndex 之间的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42488755/
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
VBA Word: Difference between Font.TextColor and Font.ColorIndex?
提问by Pookye
I have a macro that insert text. It was working well so far but... Now for some documents, I get an error 445 when it applies color. Here is the code:
我有一个插入文本的宏。到目前为止,它运行良好,但是...现在对于某些文档,应用颜色时出现错误 445。这是代码:
'Some code before that insert a first page with a different section and writes into the header
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
Selection.TypeParagraph
With Selection.Font
.Name = "Calibri"
.Size = 14
.Bold = True
.Italic = False
.TextColor = RGB(68, 114, 196)
End With
With Selection.ParagraphFormat
.Alignment = wdAlignParagraphCenter
.SpaceAfter = 6
End With
Selection.TypeText Text:="eReference file for work order: "
ActiveDocument.Bookmarks.Add Range:=Selection.Range, Name:="workorder"
Selection.TypeParagraph
I've notice that if I change "Selection.Font.TextColor = RGB(68, 114, 196)" and replace it by "Selection.Font.ColorIndex = wdDarkBlue", it works. Hence my question: What's the difference between the two? Why is there some document for which "Textcolor"doesn't work ?
我注意到,如果我更改“Selection.Font.TextColor = RGB(68, 114, 196)”并将其替换为“Selection.Font.ColorIndex = wdDarkBlue”,它会起作用。因此我的问题是:两者之间有什么区别?为什么有些文档“Textcolor”不起作用?
Thank you !
谢谢 !
回答by Mathieu Guindon
Font.TextColorand Font.ColorIndexare both documented on MSDN.
Font.TextColor和Font.ColorIndex都记录在 MSDN 上。
ColorIndex
颜色索引
Returns or sets a WdColorIndexconstant that represents the color for the specified font. Read/write.
返回或设置表示指定字体颜色的WdColorIndex常量。读/写。
WdColorIndexis an enum that defines a number of predefined constants. Being an enum, its underlying value is a numeric value - a Long
integer. When you assign it to the result of a RGB
function call, you're giving it a Long
integer, but not a WdColorIndex
value - I very much doubt that the color you're getting matches the RGB value you've set.
WdColorIndex是一个枚举,它定义了许多预定义的常量。作为一个枚举,它的底层值是一个数值 - 一个Long
整数。当您将它分配给RGB
函数调用的结果时,您给它的是一个Long
整数,而不是一个WdColorIndex
值 - 我非常怀疑您获得的颜色是否与您设置的 RGB 值匹配。
TextColor
文字颜色
Returns a ColorFormatobject that represents the color for the specified font. Read-only.
返回一个ColorFormat对象,该对象表示指定字体的颜色。只读。
A ColorFormatobject gives you much more control on how you want to format things. It's read-only because it's an object- that doesn't mean you can't change it (as in, modify its state), it only means you can't Set
that object reference to something else... but you wouldn't need to do that anyway.
一个ColorFormat对象给你你想要如何格式化的东西更多的控制。它是只读的,因为它是一个对象——这并不意味着你不能改变它(比如,修改它的状态),它只是意味着你不能Set
那个对象引用其他东西......但你不会无论如何都需要这样做。
So instead of this:
所以而不是这个:
.TextColor = RGB(68, 114, 196)
.TextColor = RGB(68, 114, 196)
You could do that:
你可以这样做:
.TextColor.RGB = RGB(68, 114, 196)
ColorFormat.RGB Property on MSDN.
FWIW I'm getting run-time error 5843 when trying to assign a non-WdColorIndex
enum value to Font.ColorIndex
, so I'm confused with what you mean by "it works" - especiallygiven that IntelliSensegives you the possible values for it:
FWIW 在尝试将非WdColorIndex
枚举值分配给 时,我收到运行时错误 5843 Font.ColorIndex
,所以我对“它有效”的意思感到困惑 -特别是考虑到IntelliSense为您提供了可能的值: