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:
'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 ?
Thank you !
Easy change To open the VBA Window press Alt + F11. Click the Tools menu and then Options – see image below. Click the Editor Format tab and change the Size drop down to 14, or whatever you want – see image below. Click OK and the font size will now be increased in the code window.
Font.TextColor and Font.ColorIndex are both documented on MSDN.
ColorIndex
Returns or sets a WdColorIndex constant that represents the color for the specified font. Read/write.
WdColorIndex is 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.
TextColor
Returns a ColorFormat object that represents the color for the specified font. Read-only.
A ColorFormat object 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.
So instead of this:
.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" - especially given that IntelliSense gives you the possible values for it:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With