Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use VBA to change colors of slide elements in Powerpoint 2013

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,

like image 943
Henrik Söderlund Avatar asked Mar 18 '15 05:03

Henrik Söderlund


People also ask

Can you color code slides in PowerPoint?

Click on the "Chart Tools: Design" tab and then click one of the multicolor options in the Chart Styles section on the right side of the Office ribbon. A color-coded legend is generated automatically.

Can VBA be used in PowerPoint?

Macros can be created for PowerPoint using Visual Basic for Applications (VBA). A presentation that contains VBA macros must be saved with a different file-name extension than a regular presentation file (such as . pptx, .

How do you change the color of an element in PowerPoint?

Click on the image and, then, on the element. Click on Shape Fill. Choose a Theme color (the first row of hues). Done!


2 Answers

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
like image 130
Steve Rindsberg Avatar answered Sep 24 '22 00:09

Steve Rindsberg


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.

like image 44
Guido Avatar answered Sep 24 '22 00:09

Guido