Skip to content
Tutorials Center
My learning

Topics

Lesson 36 of 40 ยท Design, Accessibility and Automation

Automating PowerPoint with VBA Macros

Automate repetitive PowerPoint work with VBA: enable the Developer tab, record limits, the object model, loop over slides and shapes, save as .pptm and security.

When you repeat the same task on many slides (set alt text, rename shapes, add a footer, export each slide as an image), a macro can do it in a second. PowerPoint macros are written in VBA (Visual Basic for Applications), the same language used in Word and Excel. Unlike Excel, PowerPoint has no macro recorder, so you write code by hand against its object model.

PowerPoint cannot run on this page. The VBA below is shown as read-only code that you can paste into PowerPoint, and the two runnable examples are Python stand-ins that model the same logic. If you want a full language introduction, see the VBA tutorial.

Getting ready

  1. File > Options > Customize Ribbon, tick Developer.
  2. Press Alt+F11 to open the Visual Basic Editor (VBE).
  3. In the VBE, Insert > Module creates a place for code.
  4. Save the file as PowerPoint Macro-Enabled Presentation (.pptm) so the code is kept.
  5. Run a macro with F5 in the VBE, or from Developer > Macros (Alt+F8).

When someone opens a .pptm, PowerPoint blocks macros until they click Enable Content. Only enable macros from sources you trust, because a macro can do anything you can do. File > Options > Trust Center > Trust Center Settings > Macro Settings controls the policy.

The object model

flowchart TD
  A["Application"] --> P["Presentation<br/>(ActivePresentation)"]
  P --> S["Slides"]
  S --> SL["Slide"]
  SL --> SH["Shapes"]
  SH --> SHP["Shape"]
  SHP --> TF["TextFrame.TextRange<br/>(Text, Font)"]
  P --> M["SlideMaster"]

Key objects: ActivePresentation, Slides(n), Shapes, Shape.HasTextFrame, Shape.TextFrame.TextRange.Text, Shape.AlternativeText, Slide.SlideIndex, Presentation.SlideMaster.

Example 1: list every shape on every slide

VB
Sub ListShapes()
    Dim sld As Slide
    Dim shp As Shape
    For Each sld In ActivePresentation.Slides
        For Each shp In sld.Shapes
            Debug.Print "Slide " & sld.SlideIndex & ": " & shp.Name & " (type " & shp.Type & ")"
        Next shp
    Next sld
End Sub

Debug.Print writes to the Immediate window (Ctrl+G in the VBE).

Example 2: replace text in the whole deck

VB
Sub ReplaceInDeck()
    Dim sld As Slide
    Dim shp As Shape
    For Each sld In ActivePresentation.Slides
        For Each shp In sld.Shapes
            If shp.HasTextFrame Then
                shp.TextFrame.TextRange.Replace FindWhat:="2025", ReplaceWhat:="2026"
            End If
        Next shp
    Next sld
End Sub

Example 3: add alt text to pictures that lack it

VB
Sub FlagMissingAltText()
    Dim sld As Slide
    Dim shp As Shape
    For Each sld In ActivePresentation.Slides
        For Each shp In sld.Shapes
            If shp.Type = msoPicture Then
                If Len(shp.AlternativeText) = 0 Then
                    shp.AlternativeText = "TODO: describe " & shp.Name
                End If
            End If
        Next shp
    Next sld
End Sub

Add error handling (On Error GoTo Handler) and a confirmation MsgBox for larger macros. Tables, groups and placeholders need extra checks (shp.HasTable, shp.Type = msoGroup, GroupItems).

Python stand-ins for the same logic

Python Loop over slides and shapes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python Replace text and flag pictures without alt text

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Good practice

  • Keep a backup before running a macro; Undo (Ctrl+Z) after a macro does not always restore everything.
  • Use meaningful names (ReplaceInDeck), Option Explicit at the top, and comments.
  • Test on a copy of the deck.
  • Prefer built-in features (Reuse Slides, Replace Fonts, Inspect Document) when they exist.
  • Macros are blocked in PowerPoint for the web and in most mobile apps.

Common mistakes

  • Saving as .pptx and losing all code.
  • Looping over shapes without checking HasTextFrame, which causes errors.
  • Deleting items while looping forward (loop backward with For i = Count To 1 Step -1).
  • Running macros from untrusted decks.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which file type keeps VBA code in a presentation?

    Only .pptm (macro-enabled) stores VBA projects.
  2. 2. Which shortcut opens the Visual Basic Editor?

    Alt+F11 opens the VBE; Alt+F8 lists macros; Alt+F10 is the Selection Pane.
  3. 3. What should you check before reading a shape's text in VBA?

    Pictures and some shapes have no text frame; reading it would raise an error.
  4. 4. Does PowerPoint have a macro recorder?

    PowerPoint has no recorder; you code against the object model.

Summary

  • Enable the Developer tab, open the VBE with Alt+F11, save as .pptm.
  • The object model is Presentation > Slides > Shapes > TextFrame.
  • Loop with For Each, test HasTextFrame and Type, and work on a copy.
  • Only enable macros from trusted sources.

Next: Inside a .pptx file