I'm writing a VBA Powerpoint macro to change the fill color of a text box to a shade of my Office theme's Accent 1 color. If I wanted to make the fill color the Accent 1 color itself, I would write it like this:
ActivePresentation.Slides(1).Shapes(1).Fill.ForeColor.SchemeColor = MsoThemeAccent1
However, instead of the Accent 1 color, I want the auto-generated "Accent 1, Lighter 80%" shade from the palette. Here is the accent shade I'm trying to access with my code.
I want to write the macro so the color is always the "Lighter 80%" shade value of the Accent color, so that it's dynamic if a different theme is chosen (i.e. don't want to write it as a fixed RGB value). The problem is that I don't see these shade values enumerated in the ColorScheme Index, and these shades are not uniform .TintAndShade values across themes. Is there a way to call a theme's accent colors' shades by name rather than RGB code?
CodePudding user response:
You cannot do this with one command, you need to set 2 properties of ForeColor
: The SchemeColor
-property (as you do already) and the TintAndShade
-property. You set the TintAndShade-Value for 80% as value 0.8
:
With ActivePresentation.Slides(1).Shapes(1).Fill.ForeColor
.SchemeColor = msoThemeAccent1
.TintAndShade = 0.8
End With
Set TintAndShade to 0 will set the color to the "original" Theme color, set it to 1 (=100%) will result in white. You can use any value between 0 and 1 for the TintAndShade-value.
Setting the color using ForeColor (and TintAndShade) will automatically adjust the RGB value. However, it's not vice versa when you use the RGB-property to set the color: the ForeColor-property is set to 0 when you do so. This has a consequence when you change the Design-Palette: When you used ForeColor, the colors will be adapted to the new palette. When you used RGB, the color will stay the same.
Hint: To figure those things out, switch to Excel and use the Macro recorder. The shape model is the same for both Excel and Powerpoint and you can see in the generated code which properties you have and how to use them.
Update: I thought I understood colors in VBA, turns out I missed something. Stupid me did a shortcut and formatted a cell in Excel, not a shape. Found out that using the exact same values for SchemeColor and TintAndShade on a cell and on a Shape result in different colors... To get the "80% lighter" into a Shape, you have to use the property Brightness
. The TintAndShade-value should be set to 0.
Also, instead of using .SchemeColor
, you can use .ObjectSchemeColor
- however, I didn't notice any difference using the one or the other.
With ActivePresentation.Slides(1).Shapes(1).Fill.ForeColor
.ObjectThemeColor = msoThemeAccent1
.TintAndShade = 0
.Brightness = 0.8
End With