我有一个插入文本的宏。到目前为止,它运行良好,但是...现在对于某些文档,应用颜色时出现错误 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
Run Code Online (Sandbox Code Playgroud)
我注意到,如果我更改“Selection.Font.TextColor = RGB(68, 114, 196)”并将其替换为“Selection.Font.ColorIndex = wdDarkBlue”,它会起作用。因此我的问题是:两者之间有什么区别?为什么有些文档“Textcolor”不起作用?
谢谢 !
Font.TextColor和Font.ColorIndex都记录在 MSDN 上。
颜色索引
返回或设置表示指定字体颜色的WdColorIndex常量。读/写。
WdColorIndex是一个枚举,它定义了许多预定义的常量。作为一个枚举,它的底层值是一个数值 - 一个Long整数。当您将它分配给RGB函数调用的结果时,您给它的是一个Long整数,而不是一个WdColorIndex值 - 我非常怀疑您获得的颜色是否与您设置的 RGB 值匹配。
文字颜色
返回一个ColorFormat对象,该对象表示指定字体的颜色。只读。
一个ColorFormat对象给你你想要如何格式化的东西更多的控制。它是只读的,因为它是一个对象——这并不意味着你不能改变它(比如,修改它的状态),它只是意味着你不能Set那个对象引用其他东西......但你不会无论如何都需要这样做。
所以而不是这个:
Run Code Online (Sandbox Code Playgroud).TextColor = RGB(68, 114, 196)
你可以这样做:
.TextColor.RGB = RGB(68, 114, 196)
Run Code Online (Sandbox Code Playgroud)
FWIW 在尝试将非WdColorIndex枚举值分配给 时,我收到运行时错误 5843 Font.ColorIndex,所以我对“它有效”的意思感到困惑 -特别是考虑到IntelliSense为您提供了可能的值:
