PowerPoint VBA Macro Examples & Tutorial
In this Article
- PowerPoint VBA (Macros) Tutorial
- Save As Macro-Enabled Presentation
- Enable 'Developer' Tab in the Ribbon
- Create PowerPoint Macro
- PowerPoint Application
- Open a New Presentation
- Open an Existing Presentation
- Open and Assign to a Variable
- Refer to Active Presentation
- Save Current Presentation
- Close Current Presentation
- Useful References
- Assign Existing Presentation (by name) to Variable
- Assign Active Slide to Variable
- Assign slide by Index to Variable
- Count Number of Slides
- Get Slide Index Number of Current Slide
- Add a Blank Slide to End of Slide Show
- Add a slide after current slide
- Delete a Slide
- Go to a Specific Slide
- Move Slide
- Loop Through All Slides
- Loop through All Shapes of Active Slide
- Loop through All shapes in All Slides
- Loop through All TextBoxes of Active Slide
- Loop through All TextBoxes in All Slides
- Copy Selected slides to new PPT Presentation
- Copy Active Slide to End of Active Presentation
PowerPoint VBA (Macros) Tutorial
Save As Macro-Enabled Presentation
The Presentation with VBA code should be 'Saved As' PowerPoint Macro-Enabled Presentation (*.pptm)
Enable 'Developer' Tab in the Ribbon
You should to enable the Developer tab on the Ribbon before creating VBA code. To do so choose File –> Options then click on 'Customize Ribbon' and check the box next to 'Developer" tab in the right pane.
Create PowerPoint Macro
This is a simple example of a PowerPoint VBA Macro:
Sub SavePresentationAsPDF()
Dim pptName As String
Dim PDFName As String
' Save PowerPoint as PDF
pptName = ActivePresentation.FullName
' Replace PowerPoint file extension in the name to PDF
PDFName = Left(pptName, InStr(pptName, ".")) & "pdf"
ActivePresentation.ExportAsFixedFormat PDFName, 2 ' ppFixedFormatTypePDF = 2
End Sub
It saves the active presentation as a PDF. Each line of code does the following:
- Creates variables for the PowerPoint name and PDF name
- Assigns the active presentation name to pptName variable
- Creates the full PDF name
- Saves the presentation as a PDF
PowerPoint Application
When VBA code is running within a PowerPoint Presentation, PowerPoint Application is the default application and it can be manipulated without explicitly reference. Create a New Presentation
To create a presentation, use the Add method of PowerPoint application.
Application.Presentations.Add
' or without explicit reference
Presentations.Add
Open a New Presentation
To open a new and blank presentation use the Add method of Application.Presentations collection
Presentations.Add
Open an Existing Presentation
To open a presentation which you have already created, use the Open method of Application.Presentations collection
Presentations.Open ("My Presentation.pptx")
The code above assumes that the presentation is in the same directory as the PowerPoint Presentation containing the code.
Open and Assign to a Variable
You should assign the presentation you open to a variable so that you can manipulate it as per your requirements.
Dim ppt As Presentation
Set ppt = Presentations.Open("My Presentation.pptx")
Refer to Active Presentation
Use the reference ActivePresentation to manipulate the Presentation active in the GUI when the VBA code is executed.
' Print the name of the ActivePresentation to the Immediate Window
Debug.Print ActivePresentation.Name
Save Current Presentation
The statement below will save the Active Presentation if it was saved before. It it has not been saved then you will be prompted with the 'Save As' dialog.
ActivePresentation.Save
Close Current Presentation
The statement below will close the Active Presentation even if it was not saved after the last edit.
ActivePresentation.Close
Useful References
Assign Existing Presentation (by name) to Variable
Dim myPresentationByName As Presentation
Set myPresentationByName = Application.Presentations("My Presentation")
Assign Active Slide to Variable
Dim currentSlide As Slide
Set currentSlide = Application.ActiveWindow.View.Slide
Assign slide by Index to Variable
Dim mySlide As Slide
Set mySlide = ActivePresentation.Slides(11)
Count Number of Slides
Dim slideCount As Long
slideCount = ActivePresentation.Slides.Count
Get Slide Index Number of Current Slide
Dim currentSlideIndex As Integer
currentSlideIndex = Application.ActiveWindow.View.Slide.SlideIndex
Add a Blank Slide to End of Slide Show
Dim slideCount As Long
Dim newSlide as Slide
slideCount = ActivePresentation.Slides.Count
Set newSlide = ActivePresentation.Slides.Add(slideCount + 1, 12)
' or as ppLayoutBlank = 12
Set newSlide = ActivePresentation.Slides.Add(slideCount + 1, ppLayoutBlank)
Add a slide after current slide
Dim newSlide As Slide
Dim currentSlideIndex as Integer
currentSlideIndex = Application.ActiveWindow.View.Slide.SlideIndex
Set newSlide = ActivePresentation.Slides.Add(currentSlideIndex, ppLayoutBlank)
Delete a Slide
Dim currentSlideIndex as Integer
currentSlideIndex = Application.ActiveWindow.View.Slide.SlideIndex
ActivePresentation.Slides(currentSlideIndex).Delete
Go to a Specific Slide
' This will take you to slide number 4
Application.ActiveWindow.View.GotoSlide (4)
Move Slide
You can move a slide from its old position to the new position
' Move from slide 3 to first slide
Dim oldPosition as integer, dim newPosition as integer
oldPosition = 3
newPosition = 1
ActivePresentation.Slides(oldPosition).MoveTo toPos:=newPosition
Loop Through All Slides
You can do something with each slide or go through all slides to find a few slides and do something about with using the code;
Dim mySlide as Slide
For Each mySlide In ActivePresentation.Slides
' Do something with the current slide referred to in variable 'mySlide'
' Debug.Print mySlide.Name
Next Slide
Loop through All Shapes of Active Slide
The power of PowerPoint can be realized by using 'Shapes.' The code below loops through all the shapes on the current slide so that you can manipulate them as you want;
Dim currentSlide as Slide
Dim shp as Shape
Set currentSlide = Application.ActiveWindow.View.Slide
For Each shp In currentSlide.Shapes
' Do something with the current shape referred to in variable 'shp'
' For example print the name of the shape in the Immediate Window
Debug.Print shp.Name
Next shp
Loop through All shapes in All Slides
You can loop through all the shapes in the presentation by adding a loop to go through all slides.
Dim currentSlide as Slide
Dim shp as Shape
For Each currentSlide In ActivePresentation.Slides
For Each shp In currentSlide.Shapes
' Do something with the current shape referred to in variable 'shp'
Debug.Print shp.Name
Next shp
Next currentSlide
Loop through All TextBoxes of Active Slide
TextBoxes are the most often used Shape in PowerPoint presentations. You can loop through all the Text Boxes by adding a check for 'Shape Type.' TexBoxes have the shape type defined as the VBA constant msoTextBox (the numerical value of the constant is 17)
Dim currentSlide as Slide
Dim shp as Shape
Set currentSlide = Application.ActiveWindow.View.Slide
For Each shp In currentSlide.Shapes
' Check if the shape type is msoTextBox
If shp.Type = 17 Then ' msoTextBox = 17
'Print the text in the TextBox
Debug.Print shp.TextFrame2.TextRange.Text
End If
Next shp
Loop through All TextBoxes in All Slides
Again, you can loop through all the textboxes in the presentation by adding a loop to go through all slides.
Dim currentSlide as Slide Dim shp as Shape
For Each currentSlide In ActivePresentation.Slides
For Each shp In currentSlide.Shapes
' Check if the shape type is msoTextBox
If shp.Type = 17 Then ' msoTextBox = 17
' Do something with the TextBox referred to in variable 'shp'
Debug.Print shp.TextFrame2.TextRange.Text
End If
Next shp
Next currentSlide
Copy Selected slides to new PPT Presentation
To copy certain slides to a new presentations, first select the desired slides in the existing presentation and then run the code below;
Dim currentPresentation as Presentation
Dim currentSlide as Slide
Dim newPresentation as Presentation
' Save reference to current presentation
Set currentPresentation = Application.ActivePresentation
' Save reference to current slide
Set currentSlide = Application.ActiveWindow.View.Slide
' Add new Presentation and save to a reference
Set NewPresentation = Application.Presentations.Add
' Copy selected slides
Selection.Copy
' Paste it in new Presentation
NewPresentation.Slides.Paste
Copy Active Slide to End of Active Presentation
' Copy current slide
Application.ActiveWindow.View.Slide.Copy
' Paste at the end
ActivePresentation.Slides.Paste