Automatically process all slides, shapes and objects in PowerPoint and change font attributes


Target Application: Microsoft PowerPoint

Programming Language: VBA

Description: In this tutorial, we will write a VBA macro to automatically process all slides and shapes in Microsoft PowerPoint. We will refer to this macro later on, as it is the foundation for tasks we want to apply to all presentation objects. In this tutorial, the task is to change the font, but we can do a lot more than this. But more later on…

Now we can start coding. I will describe the next steps directly in the code as comments, so you don’t have to put the different parts back together at the end. This way, you can copy the entire code and run it in your PowerPoint application.
To run the macro, simply click on the green play button at the top of the VBA Editor (or press F5 on your keyboard).

' This macro is brought to you by DG | ELEVATE YOUR BUSINESS
' Author: Daniel Glöckner
' Last update: 2022/02/26
' Feel free to copy, use, adjust, and share

' Macro purpose: Automatically process all slides and shapes in PowerPoint and change font attributes

Option Explicit ' Forces all variables to be explicitly declared


Sub ProcessSlides()
' Process all slides in presentation

' Define variables
Dim sld As Slide
Dim shp As Shape

' Process slides
For Each sld In ActivePresentation.Slides
    ' Process all shapes on slide
    For Each shp In sld.Shapes
        If shp.Type = msoGroup Then
            ' Process grouped shapes
            ProcessShape shp, True
        Else
            ' Process regular shapes
            ProcessShape shp, False
        End If
    Next shp
Next sld
End Sub

Private Function ProcessShape(shp, isGroup)
' Process shape groups and shape

' Define variables
Dim itm As Object

' Process grouped shapes
If isGroup Then
    ' Iterate over grouped items
    For Each itm In shp.GroupItems
        If itm.Type = msoGroup Then
            ' Recursively re-call funtion for sub-groups
            ProcessShape shp, True
        Else
            ProcessObject itm
        End If
    Next itm
Else
    ProcessObject shp
End If
End Function

Private Function ProcessObject(shp)
' Process tables and text frames

' Define variables
Dim tabl As Table
Dim i, j As Integer

' Perform operation on text frames
If shp.TextFrame.HasText Then
    ChangeFont shp.TextFrame.TextRange
' Perform operation on tables
ElseIf shp.HasTable Then
    Set tabl = shp.Table
    ' Iterate over table rows
    For i = 1 To tabl.Rows.Count
        ' Iterate over table columns
        For j = 1 To tabl.Columns.Count
            ChangeFont tabl.Cell(i, j).Shape.TextFrame.TextRange
        Next j
    Next i
End If
End Function

Private Function ChangeFont(tf)
' Change font

' Define variables
Dim newFont As String

' Define new font
newFont = "Century Gothic"

' Set new font. We could also change other font attributes such as size, color etc.
tf.Font.Name = newFont

End Function