PowerPoint VBA Macro Examples & Tutorial

In this Article

This is a complete guide to automating PowerPoint using VBA (Visual Basic for Applications) Macros.  Below you will find many useful examples.


VBA PDF (Free Downloads)

Download our free Microsoft PowerPoint VBA Tutorial! Or VBA Tutorials for other Office Programs!

vba powerpoint tutorial pdf

Download


PowerPoint VBA (Macros) Tutorial

AutoMacro - VBA Code Generator

Save As Macro-Enabled Presentation

The Presentation with VBA code should be ‘Saved As’ PowerPoint Macro-Enabled Presentation (*.pptm)

vba powerpoint macro enabled

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.

powerpoint vba developer ribbon

Create PowerPoint Macro

This is a simple example of a PowerPoint VBA Macro:

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.

Open a New Presentation

To open a new and blank presentation use the Add method of Application.Presentations collection

Open an Existing Presentation

To open a presentation which you have already created, use the Open method of Application.Presentations collection

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.

Refer to Active Presentation

Use the reference ActivePrentation to manipulate the Presentation active in the GUI when the VBA code is executed.

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.

Close Current Presentation

The statement below will close the Active Presentation even if it was not saved after the last edit.

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

You can move a slide from its old position to the new position

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;

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;

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.

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)

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.

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;

Copy Active Slide to End of Active Presentation

Useful PowerPoint Macro Examples

Here are some useful macro examples showing how to do tasks. These will also demonstrate the concepts described above.

Change Slide During Slide Show

Change Font on All Slides in All TextBoxes

Change Case From Upper to Normal in All TextBoxes

Toggle Case between Upper and Normal in All TextBoxes

Remove Underline from Descenders

In typography, a descender is the portion of a letter that extends below the baseline of a font. In most fonts, descenders are reserved for lowercase characters such as g, j, q, p, y, and sometimes f.

When you underline text, it does not look nice under descenders. Here is the code to remove underline from all such characters g, j, p, q, and y in the whole Presentation.

Remove Animations From All Slides

Use the code below to remove all animations set in a Presentation.

Save Presentation As PDF

You can easily save Active Presentation in PDF format.

Find and Replace Text

You can find and replace text in All TextBoxes of All Slides. After the fist instance of the text you want to find (defined by findWhat) you need to loop through the Find command to find other instances, if any.

Export Slide As Image

You can export Current SLide (or any other slide) as a PNG or JPG (JPEG) or BMP image.

Resize Image To Cover Full Slide

Exit All Running Slide Shows

If you have multiple Slide Shows open at the same time then you can close all of them using the macro below.

Automating PowerPoint from Excel

You can also connect to PowerPoint though other applications (like Excel and Word). As as first step, you must refer to an instance of PowerPoint.

There are two ways of doing it – early binding and late binding .

Open PowerPoint – Early Binding

In ‘Early Binding’ you must explicitly set a reference to ‘Microsoft PowerPoint 16 Object Library’ (for MS Office 2019) in the VBE (Visual Basic Editor) using the option Tools->References.

Open PowerPoint – Late Binding

In ‘Late Binding’ application variable is declared as an object and VBA engine connects to the correct application at run time.

Make Application Visible

After setting the reference to PowperPoint application, you may need to make it visible.

Maniplulate PowerPoint

You can use all the methods to manipulate presentations, from within PowerPoint, described above from Excel by just adding the reference to PowerPoint created by you above.

For example

has to be used liked this

Close the Application

Once you have completed what you wanted to do with the PowerPoint application you must close it and should release the reference.

Copy From Excel to PowerPoint

This code will copy a range from Excel to PowerPoint:

Note: It has been kept as simple as possible to show how a range from Excel can be copied to PowerPoint using VBA.

PowerPoint VBA FAQs

What are macros in PPT?

A Macro is a general term that refers to a set of programming instructions that automates tasks. PowerPoint (PPT) Macros automate tasks in PowerPoint using the VBA programming language.

How do I use VBA in PowerPoint?

To use VBA in PowerPoint, open the VBA Editor (ALT + F11 or Developer > Visual Basic).

How do I create a Macro in PowerPoint?

1. Open the VBA Editor (ALT + F11 or Developer > Visual Basic)
2. Go to Insert > Module to create a Code Module
3. Type ‘Sub HelloWorld’ and press Enter
4. In between the lines ‘Sub HelloWorld’ and ‘End Sub’, type ‘MsgBox “Hello World!’
5. You’ve created a Macro!
6. Now press ‘F5’ to run the Macro

Written by: Vinamra Chandra

VBA Code Examples Add-in

Easily access all of the code examples found on our site.

Simply navigate to the menu, click, and the code will be inserted directly into your module. .xlam add-in.

(No installation required!)