Results 1 to 9 of 9

Thread: Excel/VBA - change font color of object?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2004
    Location
    Right here
    Posts
    275

    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

  2. #2
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    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

  3. #3
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    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

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    May 2004
    Location
    Right here
    Posts
    275

    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

  5. #5
    Addicted Member malik641's Avatar
    Join Date
    Sep 2005
    Location
    South Florida :-)
    Posts
    221

    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!




    If you find any of my posts of good help, please rate it

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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:
    1. Set oShp = ActiveSheet.Shapes("Rectangle 1")
    2.     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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  7. #7
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    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:
    1. Dim fnt As Font
    2.   Set fnt = ActiveSheet.Shapes("Rectangle 1").TextFrame.Characters.Font
    3.   fnt.Color = RGB(123, 123, 0)
    4.   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

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    May 2004
    Location
    Right here
    Posts
    275

    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

  9. #9
    Addicted Member malik641's Avatar
    Join Date
    Sep 2005
    Location
    South Florida :-)
    Posts
    221

    Re: Excel/VBA - change font color of object?

    Quote 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...




    If you find any of my posts of good help, please rate it

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width