Get our FREE VBA eBook of the 30 most useful Excel VBA macros.

Automate Excel so that you can save time and stop doing the jobs a trained monkey could do.

Claim your free eBook

Controlling Powerpoint from Excel using VBA

Controlling PowerPoint from Excel using VBA

So far in this series about using VBA to control other Office applications, we have looked at the basics of controlling other office applications and specifically how to create macros to control Word from within Excel.  Our approach for creating macros to control Word was using the Macro Recorder.  We record very short actions, copy and paste the code into the Excel VBA Editor, then adapt the code to include the references to the Word Application.  Slowly, action by action we build up our Macro using recorded code.  It would be logical to think we could use the principles we learned for Word and apply them to PowerPoint.

But, there is one big problem to using this approach with PowerPoint:  PowerPoint does not have a Macro Recorder.  We can use VBA code in PowerPoint, but there is no Macro Recorder to create it.  As a result, we cannot record PowerPoint VBA code to copy into our Excel Macro.

But the lack of PowerPoint’s Macro Recorder is not going to stop us.  If we want to control PowerPoint from within Excel we just need to find a different approach.  Here are the three methods we will consider to obtain valid PowerPoint VBA code.

  • The beauty of the Object Model (how to use Excel code to control PowerPoint)
  • Taking code from the internet
  • Using the Object Browser and IntelliSense to help write the code

However, before we get there we need to enable the Developer Ribbon in PowerPoint.  This is so we can check the code works before moving it into Excel.

Enable the PowerPoint Developer Ribbon

If you have enabled the Excel Developer menu it is the same process in PowerPoint.

In PowerPoint: File -> Options -> Customize Ribbon

Then tick the Developer Ribbon option then click OK.

Enable PowerPoint Developer Ribbon

The beauty of the Object Model

The object model is the underlying structure of all the objects within Excel.  Within VBA, a cell is an object, a chart is an object, a drawn shape is an object.  Each of these objects has methods (the actions we can do to the objects – e.g. adding or deleting) and properties (the attributes of the objects – e.g. it’s height or width).  The Object Model to control objects in Excel is similar to the Object Model in PowerPoint.

Here is some recorded Excel VBA code to create a shape and color it red.

ActiveSheet.Shapes.AddShape(msoShapeRectangle, 50, 50, 50, 50).Select
With Selection.ShapeRange.Fill
    .Visible = msoTrue
    .ForeColor.RGB = RGB(255, 0, 0)
    .Transparency = 0
End With

Here is some Powerpoint VBA code to create a shape and color it red (using the Excel VBA Code as start point).

Dim myShape As Shape
Set myShape = ActiveWindow.View.Slide.Shapes.AddShape(msoShapeRectangle, 50, 50, 50, 50)

With myShape.Fill
    .Visible = msoTrue
    .ForeColor.RGB = RGB(255, 0, 0)
    .Transparency = 0
End With

In the code below I have highlighted in red the PowerPoint code which is exactly the same as the Excel VBA code.

Dim myShape As Shape
Set myShape = ActiveWindow.View.Slide.Shapes.AddShape(msoShapeRectangle, 50, 50, 50, 50)

With myShape.Fill
    .Visible = msoTrue
    .ForeColor.RGB = RGB(255, 0, 0)
    .Transparency = 0
End With

Did you notice that most of the code was the same?  The key differences were how we referred to the application, and the presentation.  In Excel we might use “ActiveSheet” to refer to the current worksheet, but in PowerPoint we might use “ActiveWindow.View.Slide“.  Once we know this we can adapt code we have recorded in Excel.  We can change the references to the PowerPoint Object Model and the code will work.

Before you get too excited . . . this isn’t guaranteed to work with all objects.  Just because something works in Excel it does not mean it will work perfectly in PowerPoint.  But it is a method to try.

Using Early Binding, our code could be adapted as follows – changes from the code directly above colored in red.

Dim pptApp As PowerPoint.Application
Set pptApp = New PowerPoint.Application

Dim myShape As Object
Set myShape = pptApp.ActiveWindow.View.Slide.Shapes. _
AddShape(msoShapeRectangle, 50, 50, 50, 50)

With myShape.Fill
    .Visible = msoTrue
    .ForeColor.RGB = RGB(255, 0, 0)
    .Transparency = 0
End With

With just a few carefully thought out changes to the Excel code we can now control PowerPoint from within Excel.  The changes from the PowerPoint code are:

  • Reference to the PowerPoint application now included
  • All Object variables (such as shapes) must be created as Object in the Dim statement

Taking code from the internet

You have the option to take code straight from the internet – certainly not a bad idea.  The individuals who run websites which share code want you to learn by using that code, so make the most of it.

If you really want to understand what the code means that I recommend you follow the 1 golden rule:

If you want to copy code that you do not understand you must type it out yourself – do not just copy and paste.

There is not as much PowerPoint VBA code out there on the internet as Excel VBA code, but it is there.  Once you understand the Object Model it is reasonably straightforward.

PowerPoint VBA reference sources:

Using the Object Browser and IntelliSense to help write the code

The Object Browser is the library of objects in VBA.  To view the Object Browser click View -> Object Browser (or shortcut F2) from within the Visual Basic Editor.  The screenshot below shows the Object Browser window.

PowerPoint VBA Object Library

From within the Object Browser it is possible to search for the name of any object.  The Search Results window shows all the places in the object model where that object exists.  The Members window shows all the objects, methods and properties available for the object.

IntelliSense is the feature which shows the small boxes as you type VBA code (see image below).

Intellisense Window Powerpoint VBA

If we have a piece of code, and we are not sure what could come next then just scroll through the IntelliSense options (these are the same options as shown within the Object Browser).  If it’s not in the IntelliSense list then it’s not viable code.


So, there we have it.  3 ways to write valid PowerPoint VBA code.  None of this is straightforward, so when you stumble across an issue, Google is definitely your best friend.  Chances are that somebody else has had the same problem before you.

Headshot Round

About the author

Hey, I’m Mark, and I run Excel Off The Grid.

My parents tell me that at the age of 7 I declared I was going to become a qualified accountant. I was either psychic or had no imagination, as that is exactly what happened. However, it wasn't until I was 35 that my journey really began.

In 2015, I started a new job, for which I was regularly working after 10pm. As a result, I rarely saw my children during the week. So, I started searching for the secrets to automating Excel. I discovered that by building a small number of simple tools, I could combine them together in different ways to automate nearly all my regular tasks. This meant I could work less hours (and I got pay raises!). Today, I teach these techniques to other professionals in our training program so they too can spend less time at work (and more time with their children and doing the things they love).

Do you need help adapting this post to your needs?

I'm guessing the examples in this post don't exactly match your situation. We all use Excel differently, so it's impossible to write a post that will meet everybody's needs. By taking the time to understand the techniques and principles in this post (and elsewhere on this site), you should be able to adapt it to your needs.

But, if you're still struggling you should:

  1. Read other blogs, or watch YouTube videos on the same topic. You will benefit much more by discovering your own solutions.
  2. Ask the 'Excel Ninja' in your office. It's amazing what things other people know.
  3. Ask a question in a forum like Mr Excel, or the Microsoft Answers Community. Remember, the people on these forums are generally giving their time for free. So take care to craft your question, make sure it's clear and concise.  List all the things you've tried, and provide screenshots, code segments and example workbooks.
  4. Use Excel Rescue, who are my consultancy partner. They help by providing solutions to smaller Excel problems.

What next?
Don't go yet, there is plenty more to learn on Excel Off The Grid.  Check out the latest posts:

Leave a Reply

Your email address will not be published. Required fields are marked *