More Chart VBA mysteries 2003 > 2007

User avatar
ErikJan
BronzeLounger
Posts: 1254
Joined: 03 Feb 2010, 19:59
Location: Terneuzen, the Netherlands

More Chart VBA mysteries 2003 > 2007

Post by ErikJan »

This worked in 2003

Code: Select all

    With ActiveChart
        .ChartTitle.Select
        Selection.AutoScaleFont = False
        With Selection.Characters.Font   
            .Name = "Arial"
            .FontStyle = "Bold"
            .Size = 12
            .Color = 0
            .ForeColor.RGB = RGB(0, 0, 0)
        End With
    End With
And made my chart title black. Now it's blue, no matter what number I place after .Color= ... But then, the moment I stop the code and manually do any number the text turns black :hairout:

Again, in 2003 I could figure out most things simply by recoding the actions, when I record now, I get completely different code

(Oh, by the way, this one is tested with VBA2010; I'll check tomorrow what 2007 does)

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: More Chart VBA mysteries 2003 > 2007

Post by HansV »

You can't use .ForeColor here. Try

.Color = vbBlack

or

.Color = RGB(0, 0, 0)
Best wishes,
Hans

User avatar
ErikJan
BronzeLounger
Posts: 1254
Joined: 03 Feb 2010, 19:59
Location: Terneuzen, the Netherlands

Re: More Chart VBA mysteries 2003 > 2007

Post by ErikJan »

Oh yes, sorry, the ForeColor wasn't supposed to be there, I was just testing. vbBlack works fine however. But vbblack =0... so why doesn't 0 work then????????

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: More Chart VBA mysteries 2003 > 2007

Post by HansV »

Automatic type conversion fails here, and I can only explain what happens, not why.

Both vbBlack and RGB(0, 0, 0) return a long integer (i.e. 32-bit) value 0.
Apparently, using 0 here results in a short integer (i.e. 16-bit) value 0 which gets interpreted incorrectly.
You can force VBA to use a long integer:

.Color = 0&

and this will have the same (correct) result as vbBlack and RGB(0, 0, 0).

The & is the type-declaration character for data type Long.
Best wishes,
Hans

User avatar
ErikJan
BronzeLounger
Posts: 1254
Joined: 03 Feb 2010, 19:59
Location: Terneuzen, the Netherlands

Re: More Chart VBA mysteries 2003 > 2007

Post by ErikJan »

OK, that makes it at least a little less cryptic...