|
-
Sep 30th, 2005, 02:17 PM
#1
Thread Starter
Hyperactive Member
Excel/VBA - change font color of object?
I have a button on a worksheet which runs some VBA code. Most of it works fine but when it comes to a section that is supposed to change the line, fill and font color of an AutoShape ( a rectangle) containing some text, I have a problem. I can change the line color and fill color okay with the following code:
Code:
'fill color = tan
Shapes("test").Fill.ForeColor.SchemeColor = 47
'line color = black
Shapes("test").Line.ForeColor.SchemeColor = 64
However, when I programmed in this code for the font color...
Code:
Shapes("test").Font.ColorIndex = 0
...I get the error message "object doesn't support this property or method".
I take this to mean that you cannot change the font color, but this must be wrong! How can I change the font color if the object doesn't have the .Font.ColorIndex property?
If I actually select the object on the worksheet then I can record a macro that will change the color. This is what I get:
Code:
Selection.Font.ColorIndex = 0
But I don't want to have to select the object in order to change the font color.
What can I do?
*Edit* I should also mention that the code is within a worksheet code module, not a standard code module.
Thanks
-Rob
Last edited by TheRobster; Sep 30th, 2005 at 05:54 PM.
http://www.sudsolutions.com
-
Sep 30th, 2005, 03:24 PM
#2
Re: Excel/VBA - change font color of object?
Does..
Shapes("test").Font.Color = 0
Not work??
Danny
Never Think Impossible
If you find my answer helpful then please add to my reputation
-
Sep 30th, 2005, 03:55 PM
#3
Re: Excel/VBA - change font color of object?
After a quick look into this there is no Font Object on the Shapes collection, you are going to habe to select it first as this is the only way you can change the font color..
At Least thats all I've found so far..
Danny
Never Think Impossible
If you find my answer helpful then please add to my reputation
-
Sep 30th, 2005, 04:17 PM
#4
Thread Starter
Hyperactive Member
Re: Excel/VBA - change font color of object?
That appears to be the case but if so it's a bit stupid!
The object obviously has some kind of font colour property (how else would it know what font colour to display otherwise?) but I don't understand why there is no direct way to refer to it!
http://www.sudsolutions.com
-
Sep 30th, 2005, 09:09 PM
#5
Addicted Member
Re: Excel/VBA - change font color of object?
Found this with a little research. You don't have to select the shape to change the font.
I don't know why you would have to go through this route instead of what you were trying (because that seems like the LOGICAL way of performing this), but it works.
ActiveSheet.Shapes("test").TextFrame.Characters.Font.ColorIndex = 0
BAM!
-
Sep 30th, 2005, 10:01 PM
#6
Re: Excel/VBA - change font color of object?
You access it through the textframe property. You can declare a shape object for easier access to its properties.
VB Code:
Set oShp = ActiveSheet.Shapes("Rectangle 1")
oShp.TextFrame.Characters.Font.ColorIndex = 5 'Change the rectangles text to blue
Edit: Doh! Should have refreshed the page before posting. Oh well, still shows another way to set the object.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Oct 1st, 2005, 05:00 AM
#7
Re: Excel/VBA - change font color of object?
And here I was looking at TextEffect...
You don't have to use the ColorIndex with the font by the way you can specify the RGB values..
Here's another way.. refering to the Font Object
VB Code:
Dim fnt As Font
Set fnt = ActiveSheet.Shapes("Rectangle 1").TextFrame.Characters.Font
fnt.Color = RGB(123, 123, 0)
Set fnt = Nothing
Last edited by dannymking; Oct 1st, 2005 at 08:27 AM.
Danny
Never Think Impossible
If you find my answer helpful then please add to my reputation
-
Oct 2nd, 2005, 07:48 AM
#8
Thread Starter
Hyperactive Member
Re: Excel/VBA - change font color of object?
Thanks for the help guys. 
By the way, when you write the code as:
Code:
Set oShp = ActiveSheet.Shapes("Rectangle 1")
oShp.TextFrame.Characters.Font.ColorIndex = 5 'Change the rectangles text to blue
And not as:
Code:
ActiveSheet.Shapes("Rectangle 1" _
.TextFrame.Characters.Font.ColorIndex = 5 'Change the rectangles text to blue
What's the main reason for declaring a variable rather than just using the actual object reference? Is the code faster or is it just 'tidier' to use a variable? I have tended to use the full object reference in the past but noticed that most of you declare variables wherever possible.
http://www.sudsolutions.com
-
Oct 4th, 2005, 07:19 PM
#9
Addicted Member
Re: Excel/VBA - change font color of object?
 Originally Posted by TheRobster
Thanks for the help guys.
By the way, when you write the code as:
Code:
Set oShp = ActiveSheet.Shapes("Rectangle 1")
oShp.TextFrame.Characters.Font.ColorIndex = 5 'Change the rectangles text to blue
And not as:
Code:
ActiveSheet.Shapes("Rectangle 1" _
.TextFrame.Characters.Font.ColorIndex = 5 'Change the rectangles text to blue
What's the main reason for declaring a variable rather than just using the actual object reference? Is the code faster or is it just 'tidier' to use a variable? I have tended to use the full object reference in the past but noticed that most of you declare variables wherever possible.
Well...declaring variables will make VBA run faster as it doesn't have to do continuous checks to what variables should be dimensioned as.
But the difference is probably minimal....I would guess...and I said 'Guess'
I would declare that object only if I was going to perform more changes to it. If that's all you're going to do with that object (is change the font color) then I'd say don't worry about setting the variable.
...But that's just my opinion...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|