How to create and add slides to PowerPoint presentations with VBA?

A couple days ago, a reader asked about there is a way to automate creation of PowerPoint presentations from scratch and then quickly add slides to that existing presentation. I though that script might be highly in the list of the most useful PowerPoint Macros. 

As we indicated in the past, PowerPoint doesn’t include a Macro Recorder (that’s unlike Excel and Word, which both have built in recorders for Macro). Therefore, we are left with good old Visual Basic for Application that we can use to create simple Macros to automate PowerPoint. There are further alternatives for PowerPoint Automation, but those goes beyond the scope of this tutorial.

In today’s short tutorial, we’ll go over three different use cases:

  1. New presentation creation (blank or using a template)
  2. Add slides to an existing presentation.
  3. A mix of the two above mentioned cases (Create from scratch and slides addition)

Let’s get started.

Look at the Ribbon. If you don’t see the a Developer entry in the menu, before start your coding you’ll need to enable the developer tab.

After you make the Developer menu visible your Ribbon, go ahead and hit on Developer, then hit on the Visual Basic button (or simply go ahead and hit Alt+F11) to open the Visual Basic IDE.

VBA to Create PowerPoint Presentation

  1. In the VB Editor, right click on VBAProject and hit Insert.
  2. Select Module.
  3. Paste the following code in the newly create module.
Sub CreatePresentation()
Dim NewPres as Presentation
Set NewPres = Presentations.Add
NewPres.SaveAs("MyPresentation.pptx")
End Sub
  1. Hit File and then Save.
  2. To Run your code, hit Run and then pick Run Sub/User Form (or simply F5).
  3. Close the VB editor.

VBA to insert slides

  1. In the VB Editor, right click on VBAProject and hit Insert.
  2. Select Module
  3. Paste the following code in the newly create module.
Sub CreateSlide()
Dim NewSlide as Slide
'This statement ads a title slide
Set NewSlide = ActivePresentation.Slides.Add(Index:=1, Layout:=ppLayoutTitle)
'This statement adds a blank slide in the second place
Set NewSlide = ActivePresentation.Slides.Add(Index:=2, Layout:=ppLayoutBlank)
End Sub
  1. Hit File and then Save.
  2. To Run your code, hit Run and then pick Run Sub/User Form (or simply F5).
  3. Close the VB editor.

Add slides at the end of the presentation

In order to insert a new blank slide at the last position in your presentation, use the following VBA code:

Set NewSlide  = ActivePresentation.Slides.Add(ActivePresentation.Slides.Count + 1, 11)

Macro to make a new PPTX file and add slides to it

That would be just mixing up the two snippets outlined above. Here we go:

Sub CreatePres_AddSlides()
Dim NewPres as Presentation
Dim NewSlide as Slide
Set NewPres = Presentations.Add
NewPres.SaveAs("MyPresentation.pptx")
' Title Slide
Set NewSlide = ActivePresentation.Slides.Add(Index:=1, Layout:=ppLayoutTitle)
'This statement adds a blank slide in the second place
Set NewSlide = ActivePresentation.Slides.Add(Index:=2, Layout:=ppLayoutBlank)
'Save the new PowerPoint file
NewPres.SaveAs("MyPresentation.pptx")
End Sub

Get the active Powerpoint slide with VBA

Use the following code to point to the active slide in your PowerPoint presentation. Follow the instructions provided above to inset the VBA code into a module in your presentation.

Sub GetActiveSlide()
    
Dim ActiveSlide As Slide
    
    ' Here we check if the presentation is open
    If ActivePresentation Is Nothing Then
        MsgBox "No active PowerPoint presentation. Please open a presentation first.", vbExclamation
        Exit Sub
    End If
    
    ' If the pptx file exists and presentation is open then get the active slide
    On Error Resume Next
    Set ActiveSlide = ActivePresentation.Slides(ActivePresentation.SlideIndex)
    On Error GoTo 0
    
    If ActiveSlide Is Nothing Then
        MsgBox "No slide is currently selected.", vbInformation
        Exit Sub
    End If
    
    ' Now you can work with the active slide
    MsgBox "Active slide number: " & ActiveSlide.SlideIndex, vbInformation
    
   
End Sub

Generate a PowerPoint Presentation from Excel

Last case is that you would like to create a PowerPoint presentation right from an Excel spreadsheet. You can employ the same technique from Word and other Microsoft 365 Applications.

  • Link the Spreadsheet to the PowerPoint Data Model (from Tools>>References)
  • Insert a button unto your Excel spreadsheet (Developer >> Insert and then pick a Command Button).
  • Insert the VBA code below into your Spreadsheet VBA Project (directly into the Sheet or into a VBA Module)
  • Modify the script as required.
  • Run (hit F5) and test the outcome.
Sub CreatePresentationFromExcel()


Dim MyPPt As PowerPoint.Application
Dim NewPres As PowerPoint.Presentation
Dim NewSlide As Slide
Set MyPPt = CreateObject("PowerPoint.Application")

Set NewPres = MyPPt.Presentations.Add
Set NewSlide = MyPPt.ActivePresentation.Slides.Add(Index:=1, Layout:=ppLayoutTitle)

NewPres.SaveAs ("MyPresentation.pptx")
NewPres.Close

MyPPt.Quit

MsgBox ("Presentation saved")
End Sub

Enjoy 🙂