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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 12:12:06  来源:igfitidea点击:

VBA Word: Difference between Font.TextColor and Font.ColorIndex?

vbaword-vba

提问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.TextColorFont.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 Longinteger. When you assign it to the result of a RGBfunction call, you're giving it a Longinteger, but not a WdColorIndexvalue - 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 Setthat 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.

MSDN 上的 ColorFormat.RGB 属性

FWIW I'm getting run-time error 5843 when trying to assign a non-WdColorIndexenum 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为您提供了可能的值:

wdColorIndex constants

wdColorIndex 常量