Skip to Content
Course content

10.2 Creating PowerPoint Slides.

Automating Microsoft PowerPoint with VBA allows you to create and modify presentations directly from Excel or other Office applications. With VBA, you can control elements of PowerPoint presentations such as slide content, layout, formatting, and transitions, without needing to manually open PowerPoint.

In this section, we'll explore how to automate the creation of PowerPoint slides, including basic operations such as adding slides, inserting text and images, and formatting slide content.

1. Setting Up PowerPoint Automation in VBA

Before automating PowerPoint, you need to enable the Microsoft PowerPoint Object Library reference in your VBA environment.

Steps to Enable PowerPoint Object Library:

  1. Open the VBA editor in Excel (press Alt + F11).
  2. Go to Tools > References.
  3. Scroll down and check Microsoft PowerPoint xx.x Object Library (where "xx.x" represents the version number).
  4. Click OK.

Now you're ready to control PowerPoint through VBA.

2. Creating a PowerPoint Application Object

To begin automating PowerPoint, you first need to create a PowerPoint.Application object, which allows you to control PowerPoint from within your VBA code.

Example: Opening PowerPoint Application

Sub OpenPowerPoint()
    Dim pptApp As Object
    
    ' Create PowerPoint application object
    Set pptApp = CreateObject("PowerPoint.Application")
    
    ' Make PowerPoint visible
    pptApp.Visible = True
End Sub

In this example:

  • CreateObject("PowerPoint.Application") creates a new instance of PowerPoint.
  • pptApp.Visible = True makes PowerPoint visible.

3. Creating a New PowerPoint Presentation

Once you have the PowerPoint application object, you can create a new presentation and add slides to it.

Example: Creating a New PowerPoint Presentation

Sub CreateNewPresentation()
    Dim pptApp As Object
    Dim pptPresentation As Object
    
    ' Create PowerPoint application object
    Set pptApp = CreateObject("PowerPoint.Application")
    
    ' Make PowerPoint visible
    pptApp.Visible = True
    
    ' Create a new presentation
    Set pptPresentation = pptApp.Presentations.Add
End Sub

Here:

  • pptApp.Presentations.Add creates a new PowerPoint presentation.

4. Adding Slides to the Presentation

You can add new slides to a presentation using the Slides.Add method. This method allows you to specify the slide type (layout) and the position in the presentation.

Example: Adding Slides

Sub AddSlides()
    Dim pptApp As Object
    Dim pptPresentation As Object
    
    ' Create PowerPoint application object
    Set pptApp = CreateObject("PowerPoint.Application")
    
    ' Make PowerPoint visible
    pptApp.Visible = True
    
    ' Create a new presentation
    Set pptPresentation = pptApp.Presentations.Add
    
    ' Add a title slide (Slide 1)
    pptPresentation.Slides.Add 1, 1 ' 1 = ppLayoutTitle
    
    ' Add a content slide (Slide 2)
    pptPresentation.Slides.Add 2, 2 ' 2 = ppLayoutText
End Sub

In this example:

  • pptPresentation.Slides.Add 1, 1 adds a title slide.
  • pptPresentation.Slides.Add 2, 2 adds a content slide.

5. Adding Text to PowerPoint Slides

You can insert text into slides by accessing the TextFrame property of the slide's shape or placeholder.

Example: Inserting Text on a Slide

Sub InsertTextToSlide()
    Dim pptApp As Object
    Dim pptPresentation As Object
    Dim slide As Object
    
    ' Create PowerPoint application object
    Set pptApp = CreateObject("PowerPoint.Application")
    
    ' Make PowerPoint visible
    pptApp.Visible = True
    
    ' Create a new presentation
    Set pptPresentation = pptApp.Presentations.Add
    
    ' Add a title slide
    Set slide = pptPresentation.Slides.Add(1, 1) ' Title layout
    
    ' Insert text into the title placeholder
    slide.Shapes(1).TextFrame.TextRange.Text = "Welcome to VBA Automation"
    
    ' Insert text into the content placeholder
    slide.Shapes(2).TextFrame.TextRange.Text = "This slide demonstrates how to use VBA to automate PowerPoint."
End Sub

In this example:

  • slide.Shapes(1).TextFrame.TextRange.Text inserts text into the title placeholder of the slide.
  • slide.Shapes(2).TextFrame.TextRange.Text inserts text into the content placeholder.

6. Formatting Text in PowerPoint Slides

You can apply various formatting options, such as changing font size, color, and style, using the TextRange object.

Example: Formatting Text

Sub FormatTextInSlide()
    Dim pptApp As Object
    Dim pptPresentation As Object
    Dim slide As Object
    
    ' Create PowerPoint application object
    Set pptApp = CreateObject("PowerPoint.Application")
    
    ' Make PowerPoint visible
    pptApp.Visible = True
    
    ' Create a new presentation
    Set pptPresentation = pptApp.Presentations.Add
    
    ' Add a title slide
    Set slide = pptPresentation.Slides.Add(1, 1) ' Title layout
    
    ' Insert and format text in the title
    slide.Shapes(1).TextFrame.TextRange.Text = "VBA Automation"
    slide.Shapes(1).TextFrame.TextRange.Font.Size = 44
    slide.Shapes(1).TextFrame.TextRange.Font.Bold = True
    slide.Shapes(1).TextFrame.TextRange.Font.Color.RGB = RGB(255, 0, 0) ' Red
    
    ' Insert and format text in the content
    slide.Shapes(2).TextFrame.TextRange.Text = "This is an automated PowerPoint slide created with VBA."
    slide.Shapes(2).TextFrame.TextRange.Font.Size = 32
    slide.Shapes(2).TextFrame.TextRange.Font.Italic = True
    slide.Shapes(2).TextFrame.TextRange.Font.Color.RGB = RGB(0, 0, 255) ' Blue
End Sub

In this example:

  • slide.Shapes(1).TextFrame.TextRange.Font.Size changes the font size.
  • slide.Shapes(1).TextFrame.TextRange.Font.Bold makes the text bold.
  • RGB(255, 0, 0) sets the font color to red.

7. Inserting Images and Shapes

You can insert images, shapes, and other objects into your PowerPoint slides.

Example: Inserting an Image

Sub InsertImageToSlide()
    Dim pptApp As Object
    Dim pptPresentation As Object
    Dim slide As Object
    
    ' Create PowerPoint application object
    Set pptApp = CreateObject("PowerPoint.Application")
    
    ' Make PowerPoint visible
    pptApp.Visible = True
    
    ' Create a new presentation
    Set pptPresentation = pptApp.Presentations.Add
    
    ' Add a slide
    Set slide = pptPresentation.Slides.Add(1, 2) ' Text layout
    
    ' Insert an image on the slide
    slide.Shapes.AddPicture "C:\path\to\your\image.jpg", _
        MsoTriState:=msoFalse, _
        LinkToFile:=msoFalse, _
        SaveWithDocument:=msoTrue, _
        Left:=100, Top:=100, Width:=200, Height:=150
End Sub

In this example:

  • slide.Shapes.AddPicture inserts an image from a specified path.

8. Saving and Closing PowerPoint Presentations

Once the presentation is created, you can save it and close the PowerPoint application.

Example: Saving and Closing the Presentation

Sub SaveAndClosePresentation()
    Dim pptApp As Object
    Dim pptPresentation As Object
    
    ' Create PowerPoint application object
    Set pptApp = CreateObject("PowerPoint.Application")
    
    ' Make PowerPoint visible
    pptApp.Visible = True
    
    ' Create a new presentation
    Set pptPresentation = pptApp.Presentations.Add
    
    ' Add content to the slide
    pptPresentation.Slides.Add(1, 1)
    pptPresentation.Slides(1).Shapes(1).TextFrame.TextRange.Text = "PowerPoint Automation"
    
    ' Save the presentation
    pptPresentation.SaveAs "C:\path\to\your\presentation.pptx"
    
    ' Close PowerPoint
    pptPresentation.Close
    pptApp.Quit
End Sub

Here:

  • pptPresentation.SaveAs saves the presentation at the specified location.
  • pptApp.Quit closes PowerPoint.

9. Error Handling in PowerPoint Automation

Proper error handling ensures that the automation script runs smoothly and gracefully handles any issues that arise.

Example: Error Handling in PowerPoint Automation

Sub PowerPointAutomationWithErrorHandling()
    On Error GoTo ErrorHandler
    
    Dim pptApp As Object
    Set pptApp = CreateObject("PowerPoint.Application")
    
    pptApp.Visible = True
    pptApp.Presentations.Add
    pptApp.Presentations(1).SaveAs "C:\invalid\path\presentation.pptx"
    
    Exit Sub
    
ErrorHandler:
    MsgBox "Error occurred: " & Err.Description
    pptApp.Quit
End Sub

In this example:

  • If an error occurs (e.g., due to an invalid file path), the error handler displays an error message and closes PowerPoint.

10. Summary of Creating PowerPoint Slides with VBA

  • Create and control PowerPoint: Use VBA to open PowerPoint and create new presentations.
  • Add slides and content: Insert slides with various layouts, and add text, images, and shapes.
  • Formatting: Format text (size, color, bold, italic) and modify slide content.
  • Saving and closing: Save presentations and close PowerPoint programmatically.
  • Error handling: Implement error handling to prevent crashes and ensure smooth automation.

Automating PowerPoint presentations with VBA streamlines the process of creating and modifying slides, enabling users to create consistent presentations with minimal manual intervention.

Commenting is not enabled on this course.