0
votes

I am creating a PowerPoint template that allow users to edit the entire slide deck from the first slide.

I allow users to select logos, enter text, and select slide sections. Once they push update it will update the entire deck.

The next thing I want to do is allow users to enter a primary and secondary color(RGB values). Once they push the button, I want the deck to change the color of all shapes to either the primary or secondary color. I am trying to do this with the name of the shape. Is there a way to loop through all the shapes and change the color of the shape if the name has "_main" or "_secondary" in it's name?

I am not sure if this is the right way to go about it, so I'm open to other suggestions.

I appreciate the help.

1
I agree with @egerz last paragraph: this should be done by applying theme colors to the shapes, then changing the theme to change the shape colors.John Korchok

1 Answers

0
votes

This will do what you're asking:

Public Sub ReplaceColorByName(shapeNameContains As String, rgbColor As Long)

    Dim oSld As Slide
    Dim oShp As Shape

    For Each oSld In ActivePresentation.Slides
        If oSld.SlideIndex <> 1 Then
            For Each oShp In oSld.Shapes
                If InStr(1, oShp.Name, shapeNameContains, vbTextCompare) <> 0 Then
                    oShp.Fill.ForeColor.RGB = rgbColor
                End If
            Next
        End If
    Next

End Sub


Public Sub Test01()
    ReplaceColorByName "_main", RGB(0, 255, 0)
    ReplaceColorByName "_secondary", RGB(0, 0, 255)
End Sub

So in place of that Test sub, you would just take in the RGB values from your first slide, and pass along those values instead of the solid green and blue I keyed in above.

But depending on how your users interact with PPT, using layer names may not be the best way to handle this kind of global recolor task. Like, what happens if/when they create a new shape? The new shape would get a random name like "Oval 7" and most users don't even know that the Selection Pane exists -- getting them to manually rename "Oval 7" to "Oval 7_main" might be too much to ask.

In my experience tying all shapes' fills to the theme colors, and changing the theme colors as needed, is the best way to force users to use the right colors.