Controlling Powerpoint from Excel using VBA

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
    .Solid
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
    .Solid
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
    .Solid
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
    .Solid
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 has shapes) must be created as Object in the Dim statement

For a fuller description of the Object Model read my post: Understanding the code.

 

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 straight forward.

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 screen shot 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.

 

Conclusion

So, there we have it.  3 ways to write valid PowerPoint VBA code.  None of this is straight forward, 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.

 

What next?

Get Excel news, tips & tricks straight to you inbox.  Helping you to save time and achieve more with Excel.

You will also receive the Tab Hopper Add-in for FREE.

Tab Hopper Thumb







Save

Save

Save

Save

Save

Save

Leave a Reply

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