Use VBA to change colors of slide elements in Powerpoint 2013
Asked Answered
A

2

9

I have a 150-slide powerpoint presentation that I want to revise due to a company rebranding effort. Our previous turquoise color has been used on text, lines, shapes and shape fills. I would like to build a VBA script that runs across the entire presentation, and in one fell swoop amends all slides and replaces this bluish color with our new dark gray color.

The old corporate color was RGB(0, 176, 240) - turquoise

The new corporate color is RGB(71, 67, 65) - dark gray

I have tried a multitude of different vba's across the internet but can't get it to work properly. Here is a screenshot of a typical slide from the old color - all the blue items should be changed to dark gray:

screenshot

This piece of VBA code from a helpful forum member worked really well for shape fills - if this could be re-worked to include any text and shape outlines and lines as well, it would be perfect.

Sub ChangeShapeColor()

    Dim oSh As Shape
    Dim oSl As Slide

    ' Look at each slide in the current presentation:
    For Each oSl In ActivePresentation.Slides

        ' Look at each shape on each slide:
        For Each oSh In oSl.Shapes

            ' IF the shape's .Fill.ForeColor.RGB = turqoise color:
            If oSh.Fill.ForeColor.RGB = RGB(0, 176, 240) Then

                ' Change it to corporate dark grey:
            oSh.Fill.ForeColor.RGB = RGB(71, 67, 65)

            End If

        Next oSh

    Next oSl

End Sub

Thanks in advance,

Acclivity answered 18/3, 2015 at 5:57 Comment(4)
And you need to replace everything, both text and shapes? How about the pictures? I believe those are pictures?Milden
The images are white outlined images/logos, residing inside normal powerpoint shapes with fill/outlines in blue. It is the blue color I want to change. The circle and "spokes" are also normal powerpoint lines that I want new colors on. Basically everything that is blue on that screenshot is what I want to change.Analgesic
What have you tried so far and what hasn't worked properly? What happens that shouldn't, what doesn't happen that should? And as a suggestion: consider using a theme color rather than an RGB coded color; then in future all you'd need to do is change the them and PPT will automatically recolor anything assigned that theme color.Dido
Good idea with the the theme color, thanks for that tip. I will add come VBA code that worked for me up in the answer - thanks for the suggestion.Analgesic
D
11

This should get you a step closer, though I'd probably rewrite it as a function that you could pass lFindColor and lReplaceColor to.

Sub ReplaceColors()

    Dim lFindColor As Long
    Dim lReplaceColor As Long
    Dim oSl As Slide
    Dim oSh As Shape
    Dim x As Long

    lFindColor = RGB(255, 128, 128)
    lReplaceColor = RGB(128, 128, 255)

    For Each oSl In ActivePresentation.Slides
        For Each oSh In oSl.Shapes
            With oSh

                ' Fill
                If .Fill.ForeColor.RGB = lFindColor Then
                    .Fill.ForeColor.RGB = lReplaceColor
                End If

                ' Line
                If .Line.Visible Then
                    If .Line.ForeColor.RGB = lFindColor Then
                        .Line.ForeColor.RGB = lReplaceColor
                    End If
                End If

                ' Text
                If .HasTextFrame Then
                    If .TextFrame.HasText Then
                        For x = 1 To .TextFrame.TextRange.Runs.Count
                            If .TextFrame.TextRange.Runs(x).Font.Color.RGB = lFindColor Then
                                .TextFrame.TextRange.Runs(x).Font.Color.RGB = lReplaceColor
                            End If
                        Next
                    End If
                End If
            End With
        Next
    Next

End Sub
Dido answered 19/3, 2015 at 15:0 Comment(4)
Thanks for helping out. However, the script fails on the following line, complaining "value out or range": If .Line.Visible ThenAnalgesic
What kind of shape is it processing at that point? After "With oSh" add "Debug.Print .Name" (don't include the quote marks). Press Ctrl+G in the VBA IDE to display the Immediate window before running the code. The Debug.Print results will appear there.Dido
Fantastic, that worked, and I found the culprit being the "Table 1". I simply removed the few tables I had and the script completed. Would you be able to add color replacements to all "Oval", "Rectangle" and "Chart" too? Or just all shapes maybe even?Analgesic
This should already work with all of the normal shapes you add from the Insert Shape menu. Charts are a whole additional level of complexity; possible but far more work than I have time to tackle. But to prevent other problems, you could add a case selector: Select Case oSh.Type, then have it ignore shapes of type Chart, Media and so on. You'd also want to add a test for If oSh.HasChart or oSh.HasTable Then ' Skip it, don't try to recolorDido
M
1

I'd like to add another solution, not using VBA. It may be a bit hacky, but it works remarkably well. The idea is to get inside the pptx-file and perform a find-and-replace operation on color codes. This will replace all occurences of a specific color in the whole presentation (be it normal text, shading, parts of a graph, borders of a table, you name it).

STEP 1. Create a back-up of your original Powerpoint file!

STEP 2. If your presentation has the extension .ppt, open it in Powerpoint and save it again as .pptx.

STEP 3. Change the extension .pptx to .zip (and ignore any warnings by Windows). For example, change "my_presentation.pptx" to "my_presentation.zip". The reason for this is that a pptx-file is in fact a zip-file. By renaming the file to .zip you will be able to extract it.

STEP 4. Extract the zip-file. You will obtain a folder (and subfolders) that contains a lot of xml-files (and possibly other files). Somewhere in these xml-files there must be color definitions, although we don't know where exactly.

STEP 5. Determine the hexadecimal codes of the old and the new color. For example, if the old color is turquoise (rgb: 0,176,240), its hexadecimal code will be 00B0F0. If the new color is dark gray (rgb: 71,67,55), its hexadecimal code will be 474337.

STEP 6. Download, install and open the (free) text editor Notepad++. (You can find it here: https://notepad-plus-plus.org/.)

STEP 7. Within Notepad++ click "Search >> Find in Files ...". This allows you to perform a find-and-replace operation in all extracted (xml-)files at once. Select the correct folder, search for the old color code (00B0F0) and replace it with the new color code (474337). Make sure the search is performed in subfolders as well. See this screenshot.

STEP 8. Now compress the files again into a zip file. It's important that you select exactly the previously extracted files, without their parent folder. (If you accidentally compress on the level of the parent folder, you will create an extra layer in the folder hierarchy of the zip-file, which will confuse Powerpoint.)

STEP 9. Change the extension of the newly created zip file into .pttx (and ignore any warnings by Windows).

WARNING: There is a small probability that the hexadecimal color code you search for in STEP 7, shows up as something completely different as well (say a phone number or whatever). If there is any chance you will mess up your presentation this way, search for val="00B0F0" instead of just 00B0F0.

Musicale answered 28/1, 2020 at 19:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.