Brief about the Tutorial

In this tutorial, I am planning to write 3-4 articles which will be all around Microsoft Power Point Presentation but using Excel Macro. So as part of this tutorial you will basically learn how to Open, Close, save, Copy a slide from one Presentation to other, deleting a slide, modifying the content of the slide and so on..
 

Very Important:

This article has got a very detailed level of technical discussion. If you want to learn and understand the whole code, you can go through otherwise you can omit those details and copy the code or download the file and they are ready to use.
 
Since number of code snippets and downloadable excels are going to be more, that is why I thought of dividing this in to more than one article and club them as part of Tutorial.
 

Since we are going to access an application which out side Microsoft Excel. Therefore to interact with that application you should have a good understanding of Objects and Methods of that application. Here in this article I am not going to explain you about all the Objects, Methods and Properties of PowerPoint Application but few of them. To know all possible Objects, Methods, Properties and their hierarchy you can refer this page:
http://msdn.microsoft.com/en-us/library/office/aa213568(v=office.11).aspx.
 

To start with any operation with any kind of PowerPoint file, you need to create an Object for the application itself. Therefore your code will always start with a statement to create an Object for PowerPoint Application:

Sub Create_PowerPoint_Object
'Define one variable of Object Type to hold the Application Object
Dim objNewPowerPoint as Object
'Create an Object for PowerPoint Application
Set objNewPowerPoint = CreateObject("PowerPoint.Application")
objNewPowerPoint.Visible = True
End Sub

As soon as the above line of statements are executed you can see a Power Point File launched which will look like below:

Power-Point-Application-Object
 

As you can see in the above image only a simple Application is launched. It has no placeholder for presentations and Slides. This is because you have just created an Application Object.
 
Now we have an Application Object so we can create multiple Presentation File from this.
 

sub Add_Two_Presentations
'Define one variable of Object Type to hold the Application Object
Dim objNewPowerPoint As Object
Dim MyPresentation1 As Object
Dim MyPresentation2 As Object
'Create an Object for PowerPoint Application
Set objNewPowerPoint = CreateObject("PowerPoint.Application")
'Make this Application Object Visible
objNewPowerPoint.Visible = True
'Create 2 presentations from that Application Object
Set MyPresentation1 = objNewPowerPoint.Presentations.Add
Set MyPresentation2 = objNewPowerPoint.Presentations.Add
End Sub

Above code snippet will add 2 presentations (2 PowerPoint files) as you can see in the below images:

Presentations-Object
 

But these presentations will be looking something like this.
 

PowerPoint-Presentation-Object

Power Point Presentation Object

 
As you can see in the above image, there is no slide. Now let us see how to add a slide in a presentation created above.
In the above example you had added two new presentations with no slide. To add slide in those presentations you need to use the below statements:
 

MyPresentation1.Slides.Add <Index Number> , <Layout of the slide>
Where:

Index Number: Slides is basically a collection of all Slide Object a Presentation has it. Therfore to add a slide in Slides collection, you need to pass the index number which tells the position of the new slide in Slides collection. In simple terms, this indicates the position of your Slide in your PowerPoint Presentation.
For example: If you pass Index number as 3 then new slide will be added at 3rd position, no matter how many more slides are there. But if total number of slides are less than 2 and you passed index as 3 then it will throw error.

Layout of the slide:
This basically tells the type of Layout you want for your new slide. There is a list of around more than 20 layout type which you can use it for Power Point 2007. You can pass the exact VBA name of a layout or just a number from 1 to 29 or what-so-ever. I will come on this in detail in coming articles.

What did we learn so far?

1. Power Point Application , Presentations and Slides Objects
2. How to create a new blank Power Point Presentations
3. How to add slides to a presentation

 

VBA Code: Creating a New Power Point Presentation using Excel Macro

As I have explained above, I will club all the lines of code with some more statements together and form a single code which will do the following :

1. Create 2 New Power Point File
2. Add 5 slides in one presentation and 3 in second one.
3. Save these presentations on your desktop

Option Explicit

Sub Add_Two_Presentations()
'Define one variable of Object Type to hold the Application Object
Dim objNewPowerPoint As Object
Dim MyPresentation1 As Object
Dim MyPresentation2 As Object
Dim p1Slides As Object
Dim p2Slides As Object
Dim iSlide As Integer
Dim desktopURL
On Error GoTo err
'Create an Object for PowerPoint Application
Set objNewPowerPoint = CreateObject("PowerPoint.Application")
'Make this Application Object Visible
objNewPowerPoint.Visible = True
'Create 2 presentations from that Application Object
Set MyPresentation1 = objNewPowerPoint.Presentations.Add
Set MyPresentation2 = objNewPowerPoint.Presentations.Add
'Assign the Collection of Slide Objects to an Object Variable
Set p1Slides = MyPresentation1.Slides
Set p2Slides = MyPresentation2.Slides

'Now add 5 slides in first presentation and 3 in second one
For iSlide = 1 To 5
    p1Slides.Add iSlide, ppLayoutCustom
    'this will add only 3 slides to the second ppt
    If iSlide 

 
Above you have learnt how to create a new Powerpoint presentation with multiple slides in it, save and close it. Now I will teach you how to open an existing Power Point file using Excel VBA

How to open Power Point File using Excel Macro

First you need to create a PowerPoint Application object which will actually hold your opened presentation file. If you do not have a valid Power Point Application object then you can not open your presentation file directly.

&nsp;
To open a presentation you can use the below statement:

objNewPowerPoint.Presentations.Open("filepath")
Where :

objNewPowerPoint - is your defined Power Point Application Object
FilePath - This is the complete path of the file with file name

 
Above statement opens a presentation file which is a Presentation Object. Therefore we should assign this to a unique object so that at any point of program we can refer this presentation uniquely.

Dim MyPresentation3 as Object
Set MyPresentation3 = objNewPowerPoint.Presentations.Open("filepath")

Now you can use MyPresentation3 presentation object to add slide, delete, save, close etc. same as the above code.
 
Now let us learn through an example with a complete flow.
 

Example:

In this example, I am going to do the following:

1. open an Existing power point presentation from your local drive
2. Add a Slide at 3rd position (make sure that the file you choose has at least 2 slides other wise it will through as explained above)
3. Save and close the presentation

 

Note: In the first example as well we saved one presentation. But in this example Save is different. If you notice in the first example we had used SaveAs because it was a new file and we have to give a valid path and name to save it but in this example this file is already saved in a particular location and name from where you have opened it.
Therefore in this case you can simply run the Save command which will save the changes you made in that presentation. But if you still want to save this as a different file name or location you can go ahead with SaveAs command. Save As is same as the save as command in any file.

 


Sub Open_an_existing_Presentations()
'Define one variable of Object Type to hold the Application Object
Dim objNewPowerPoint As Object
Dim MyPresentation As Object
Dim pSlides As Object

On Error GoTo err
'Create an Object for PowerPoint Application
Set objNewPowerPoint = CreateObject("PowerPoint.Application")
'Make this Application Object Visible
objNewPowerPoint.Visible = True
'Open your presentation and assign it to MyPresentation Object
Set MyPresentation = objNewPowerPoint.Presentations.Open(<Comeplete path>)
'Assign the Collection of Slide Objects to an Object Variable
Set pSlides = MyPresentation.Slides
'Note: pSlides Object has all the slides in that presentation
'Now add a new slide at 3rd position in the collection of slides pSlides
pSlides.Add 3, ppLayoutCustom
'save and close the presentation
MyPresentation.Save
MyPresentation.Close
'release the memory from all the objects
Set MyPresentation = Nothing
Set pSlides = Nothing
'Finally quit the Power Point application
objNewPowerPoint.Quit
Exit Sub
err:
'if any Error occurred, display the error code and description
MsgBox err.Number & ": " & err.Description
End Sub

About the Next Article...

In this article we tried interacting with Power Point Presentations by creating the Objects using CreateObject keywords. Because of this you do not get any IntelliSense/suggestion as soon as you type any statement which VBA compiler generally pops-up. But there is another way by adding a reference of Power Point in your Excel VBA. We will cover that in my article along with some other stuffs related to Power Point Presentation - using VBA.

This is where I am closing the Part 1 of this Tutorial. If you face any issue or any suggestion regarding this article, feel free to send them by any means..even by pigeons if possible 😛