VBA PowerPoint

PowerPoint is a Microsoft application used to work with slideshow presentations. VBA is embedded in the PowerPoint application and can be used to work with PowerPoint programmatically. The PowerPoint Object Library contains classes that make working with PowerPoint in VBA possible. The classes in the PowerPoint Object Library are referred to collectively as the PowerPoint Object Model. When using VBA within PowerPoint, a reference is automatically included to the PowerPoint Object Library.

PowerPoint Object Model

The PowerPoint Object Model contains classes that make working with PowerPoint in VBA possible. There are many classes in the PowerPoint Object Model but the general overview of the PowerPoint Object Model is Application → Presentation → Slide → Shape.

  1. Application
    1. Presentations
      1. Presentation
        1. Slides
          1. Slide
            1. Shapes
              1. Shape
                1. TextFrame
                2. Table
                3. Chart
                4. SmartArt*

*The SmartArt object is defined in the Office library but is a property of the Shape object.

Context

The place where VBA code is written determines the implied context when referring to classes. For example, when using VBA in PowerPoint and referring to the Application object, it is implied that Application and PowerPoint.Application are the same object. However, when using VBA in another application to work with PowerPoint, it is necessary to qualify references to avoid naming collisions with classes in the other application. Using qualified references requires that a reference is set to the library where the classes are defined. If a reference cannot be set, late-binding can be used instead.

Public Sub Example()

    '''In PowerPoint

    Debug.Print Application Is PowerPoint.Application 'Prints: True

End Sub
Public Sub Example()

    '''In Excel

    'Reference to PowerPoint Object Library is required

    Debug.Print Application Is PowerPoint.Application 'Prints: False

    Debug.Print Application Is Excel.Application 'Prints: True

End Sub

To retrieve the PowerPoint Application object from another application, attempt to retrieve the running application and if that fails start a new instance of the application.

Public Function GetPowerPointApp() As Object

    On Error Resume Next
    Set GetPowerPointApp = GetObject(, "PowerPoint.Application")
    On Error GoTo 0

    If GetPowerPointApp Is Nothing Then
        Set GetPowerPointApp = CreateObject("PowerPoint.Application")
    End If

End Function

Application

The Application object represents the PowerPoint application itself. The Application object is the top-level object and provides access to the rest of the PowerPoint Object Model. The Application object can be used to access functionality and properties of the PowerPoint application.

Presentation

The Presentation object represents a PowerPoint presentation. All open presentations are stored in the Application object's Presentations collection object. Use the Application.Presentations property to get a Presentations collection containing all open Presentations.

Open

To open a Presentation use the Presentations.Open method.

Public Sub Example()

    Dim P As Presentation
    Set P = Presentations.Open("C:\Example.pptx")

End Sub

Create

To create a new Presentation use the Presentations.Add method.

Public Sub Example()

    Dim P As Presentation
    Set P = Presentations.Add

End Sub

Save

To save a Presentation use the SaveAs, Save, and SaveCopyAs methods of the Presentation object.

Public Sub Example()

    Dim P As Presentation
    Set P = Presentations.Add

    P.SaveAs "C:\Example.pptx"

    P.Save

    P.SaveCopyAs "C:\Example.pptx"

End Sub

Close

To close a Presentation use the Close method of the Presentation object.

Public Sub Example()

    Dim P As Presentation
    Set P = Presentations.Open("C:\Example.pptx")

    P.Save

    P.Close

End Sub

Iterate

To iterate over open Presentations use a For Next or For Each loop with the Presentations collection object.

Public Sub Example()

    Dim i As Long
    For i = 1 To Presentations.Count
        Debug.Print Presentations(i).Name
    Next i

    Dim P As Presentation
    For Each P In Presentations
        Debug.Print P.Name
    Next P

End Sub

Slides

The Slide object represents a PowerPoint slide. The Slides collection object holds a collection of Slide objects. Use the Presentation.Slides property to get the Slides collection object containing all Slide objects in a Presentation.

Add

To add a slide use the Slides.AddSlide method.

Public Sub Example()

    'Add new slide to end of slides

    Dim CL As CustomLayout
    Set CL = Presentations(1).Slides(1).CustomLayout

    Dim S As Slide
    Set S = Presentations(1).Slides.AddSlide(Presentations(1).Slides.Count + 1, CL)

End Sub

Copy

To copy a Slide use the Slide.Copy method with the Slides.Paste method.

Public Sub Example()

    'Copy slide and past at end of slides

    Presentations(1).Slides(1).Copy

    Presentations(1).Slides.Paste

End Sub

Cut

To cut a slide use the Slide.Cut method with the Slides.Paste method.

Public Sub Example()

    'Cut slide and past at end of slides

    Presentations(1).Slides(1).Cut

    Presentations(1).Slides.Paste  

End Sub

Move

To move a slide use the MoveTo method of the Slide object.

Public Sub Example()

    Presentations(1).Slides(1).MoveTo Presentations(1).Slides.Count

End Sub

Duplicate

To duplicate a slide use the Duplicate method of the Slide object.

Public Sub Example()

    Presentations(1).Slides(1).Duplicate

End Sub

Delete

To delete a slide use the Delete method of the Slide object.

Public Sub Example()

    Presentations(1).Slides(1).Delete

End Sub

Iterate

Slides can be iterated over using a For Next or For Each loop.

Public Sub Example()

    Dim P As Presentation
    Set P = Presentations(1)

    Dim i As Long
    For i = 1 To P.Slides.Count
        Debug.Print P.Slides(i).Name
    Next i

    Dim S As Slide
    For Each S In P.Slides
        Debug.Print S.Name
    Next S

End Sub

Shapes

The Shape object represents a shape. The Shapes collection object contains a collection of Shape objects. Use the Slide.Shapes property to get a Shapes collection contains all Shape objects on a Slide. Shapes can contain various types of objects.

Retrieving Shapes

Shape objects can be retrieved from the Shapes collection by their index number or their Name property.

Public Sub Example()

    Dim P As Presentation
    Set P = Presentations(1)

    Dim S As Slide
    Set S = P.Slides(1)

    Dim SH As Shape

    'Get shape by index number
    Set SH = S.Shapes(1)
    SH.Name = "ExampleShape1"

    'Get shape by name
    Set SH = S.Shapes("ExampleShape1")

End Sub

Creating Shapes

Shapes can be created by calling one of the Add methods on the Shapes collection. There are a number of add methods and among them are AddShape, AddTextbox, AddTable, AddChart2, AddSmartArt, and AddPicture.

Public Sub Example()

    Dim P As Presentation
    Set P = Presentations(1)

    Dim S As Slide
    Set S = P.Slides(1)

    S.Shapes.AddShape msoShapeRectangle, 0, 0, 200, 100

    S.Shapes.AddTextbox msoTextOrientationHorizontal, 0, 0, 200, 100

    S.Shapes.AddTable 5, 5

    S.Shapes.AddChart2 Type:=xlPie

    S.Shapes.AddSmartArt _
    Application.SmartArtLayouts("urn:microsoft.com/office/officeart/2005/8/layout/orgChart1")

    S.Shapes.AddPicture "C:\Example.png", msoFalse, msoCTrue, 0, 0

End Sub

Determining the Type of a Shape

The Shape object contains a number of properties that can be used to determine what type of objects the Shape contains. These properties include HasTextFrame, HasTable, HasChart, and HasSmartArt.

Public Sub Example()

    Dim P As Presentation
    Set P = Presentations(1)

    Dim S As Slide
    Set S = P.Slides(1)

    Dim SH As Shape
    Set SH = S.Shapes(1)

    If SH.HasTextFrame Then
        Debug.Print "HasTextFrame"
    End If

    If SH.HasTable Then
        Debug.Print "HasTable"
    End If

    If SH.HasChart Then
        Debug.Print "HasChart"
    End If

    If SH.HasSmartArt Then
        Debug.Print "HasSmartArt"
    End If

End Sub

TextFrame

The TextFrame object represents a frame containing text on a slide.

Public Sub Example()

    Dim P As Presentation
    Set P = Presentations(1)

    Dim S As Slide
    Set S = P.Slides(1)

    Dim SH As Shape
    Set SH = S.Shapes(1)

    If SH.HasTextFrame Then
        SH.TextFrame.TextRange.Text = "Hello, World!"
    End If

End Sub

Table

The Table object represents a table in PowerPoint. To add a table use the Shapes.AddTable method.

Public Sub Example()

    Dim P As PowerPoint.Presentation
    Set P = PowerPoint.Application.Presentations(1)

    Dim S As PowerPoint.Shape
    Set S = P.Slides(1).Shapes.AddTable(10, 3, 0, 0, 100, 100)

    Dim T As PowerPoint.Table
    Set T = S.Table

End Sub

Charts

The Chart object represents a chart in PowerPoint. To create a chart use the Shapes.AddChart2 method.

Public Sub Example()

    Dim P As PowerPoint.Presentation
    Set P = PowerPoint.Application.Presentations(1)

    Dim S As PowerPoint.Shape
    Set S = P.Slides(1).Shapes.AddChart2

    Dim C As PowerPoint.Chart
    Set C = S.Chart

End Sub

SmartArt

The SmartArt object represents SmartArt. The Shape.SmartArt property contains the Shape's SmartArt object if it has one. The Shape.HasSmartArt property can be used to detect if a Shape has a SmartArt object. SmartArt can be added to a slide using the Shapes.AddSmartArt method. The AddSmartArt method requires a Layout parameter which is a SmartArtLayout object. To create a SmartArtLayout object pass the Id or Index of the desired layout to the Application.SmartArtLayouts property.

Public Sub Example()

    Dim LayoutId As String
    LayoutId = "urn:microsoft.com/office/officeart/2005/8/layout/orgChart1"

    Dim Layout As SmartArtLayout
    Layout = PowerPoint.Application.SmartArtLayouts(LayoutId)

    Dim S As PowerPoint.Slide
    Set S = PowerPoint.Application.Presentations(1).Slides(1)
    S.Shapes.AddSmartArt Layout

End Sub