Complete VBA tutorial to interact with Power point Presentations – Part – 1

.

[fusion_text]D

ear Friends,

First of all, I apologize for not responding to many of your questions around dealing with PowerPoint presentations through Excel Macro. Many of you have sent me so many questions around this topic. Questions, which were, mostly, asked were like –

Sample List of Questions…

  VBA code to create a presentation slide based on a Table in Excel
  Excel macro to create a Slide with Graph and Table in Excel
  Macro to paste Graph from Excel in to a PPT in a specific Slide
  VBA code to remove old graph from a specific slide and place the new generated graph in Excel – Like refresh button

This list i just a summary what has been asked so far. Many of the questions were too specific, hence I have not mentioned them here. Therefore, I thought of writing a tutorial (more than a single article) and cover most of the aspects related to interaction with PowerPoint presentations through Exel VBA. Rather putting everything in one article, I am splitting in to more than one article. As part of this tutorial, I am sure, you will learn all the basic things (regular things) to interact with PowerPoint Presentations through Excel VBA.
Furthermore, you will have readily available VBA code snippets for all the basic operations, one can perform on Presentations like Open, Close, save, Copy a slide from one Presentation to other, deleting a slide, modifying the content of the slide and so on..
Finally, at the end of this tutorial, you will find a FREE Excel VBA tool to download. Mostly in the next article of this tutorial.

Topics covered in this Article

Since we are going to access an application which is 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 make you comfortable. 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.
 

VBA to Create a New Power Point Application

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 below – an Empty Presentations without any Slide in it.

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.

VBA code to add slides to PowerPoint Presentation

In the above example you had added two new presentations with no slide. As you already know, each presentation file may contain multiple slides in it. That means, a Presentatio Object holds a collection of Slides. This means…You can add slides to Slides collection using .Add method of Slides object (Collection of Slide).
Refer the Syntax of this method below

Syntax of .Add Method

.Add method belongs to Slides Object.
[highlight color=”yellow”]MyPresentation1.Slides.Add <Index Number> , <Layout of the slide>[/highlight]

Where:

Index Number:
Slides is basically a collection of all Slide Object, a Presentation has it. Therefore 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 an 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 an integer number (depending on the version of the Office you have in your computer)


Sub Add_slide_to_PowerPoint()
'Define one variable of Object Type to hold the Application Object
	Dim objNewPowerPoint As PowerPoint.Application
	Dim MyPresentation As Presentation
	Dim pSlides As Slides
	On Error GoTo err
'Create an Object for PowerPoint Application
	Set objNewPowerPoint = New PowerPoint.Application
	
'Make this Application Object Visible
	objNewPowerPoint.Visible = True
'Open your presentation and assign it to MyPresentation Object
	Set MyPresentation = objNewPowerPoint.Presentations.Open(&quot;C:\Users\vmishra\Desktop\PPT-Slides.pptx&quot;)
'Assign the Collection of Slide Objects to an Object Variable
	Set pSlides = MyPresentation.Slides
' Note: pSlides Object has all the slides in that presentation
' Below statement will add a new slide to the collection
' at the given location, e.g. at 3rd position.
' SlideLayout is given as Blank slide layout
	pSlides.Add 3, ppLayoutBlank
'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 &amp; &quot;: &quot; &amp; err.Description
End Sub

Save & Close PowerPoint Through Excel VBA

Let’s have a look – on how to save and close a power point presentation using Excel VBA.
.Save and .Close is the keywords to save and close a presentation respectively. These methods belongs to Presentation Object.


	Dim objNewPowerPoint As PowerPoint.Application
	Dim MyPresentation As Presentation
	
	Set objNewPowerPoint = New PowerPoint.Application
	'Open your presentation and assign it to MyPresentation Object
	Set MyPresentation = objNewPowerPoint.Presentations.Open(&quot;C:\Users\...\Desktop\PPT-Slides.pptx&quot;)
	'Save the presentation 
	MyPresentation.Save
	
	'Close the presentation
	MyPresentation.Close

In case you want to save your presentation with a different name or at different location then you can use .SaveAs method. The only difference in both these methods is – For .Save you do not need to provide file path but for .SaveAs it is mandatory to provide the file path. Refer below code.


	Dim objNewPowerPoint As PowerPoint.Application
	Dim MyPresentation As Presentation
	
	Set objNewPowerPoint = New PowerPoint.Application
	'Open your presentation and assign it to MyPresentation Object
	Set MyPresentation = objNewPowerPoint.Presentations.Open(&quot;C:\Users\...\Desktop\PPT-Slides.pptx&quot;)
	
	'SaveAs the presentation at different location with different name
	MyPresentation.SaveAs Filename:=desktopURL &amp; &quot;C:\..\..\Desktop\PPT-Slides-New.pptx&quot;

What did we learn so far?

1. How to create Power Point Application , Presentations and Slides Objects
2. How to create new Power Point Presentations
3. How to add slides to power point presentation
4. How to Save and Close Presentations in Excel VBA

VBA to Create a New Power Point Presentation

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 power point presentation files 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(&quot;PowerPoint.Application&quot;)
'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 &lt;= 3 Then
        p2Slides.Add iSlide, ppLayoutCustom
    End If
Next

'get the desktop path
desktopURL = CreateObject(&quot;WScript.Shell&quot;).specialfolders(&quot;Desktop&quot;)
'save both the ppts on the desktop
MyPresentation1.SaveAs Filename:=desktopURL &amp; &quot;\ppt1.pptx&quot;
MyPresentation2.SaveAs Filename:=desktopURL &amp; &quot;\ppt2.pptx&quot;

'close both the presentations
MyPresentation1.Close
MyPresentation2.Close

'release the memory from all the objects
Set MyPresentation1 = Nothing
Set MyPresentation2 = Nothing
Set p1Slides = Nothing
Set p2Slides = 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 &amp; &quot;: &quot; &amp; err.Description
End Sub

 
You have learnt above, 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

Excel VBA to open PowerPoint Presentation File

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.

To open a presentation you can use the below statement:


objNewPowerPoint.Presentations.Open(&quot;filepath&quot;)

Where :

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

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(&quot;filepath&quot;)

Now you can use MyPresentation3 presentation object to add slides to it, delete, save, close etc. same as the above code.

Example:

Now let us create an example with everything we learnt so far. In this example, I am going to do the following:

1. open an Existing power point presentation
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 an exception as explained above)
3. Save and close the presentation



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(&quot;PowerPoint.Application&quot;)
'Make this Application Object Visible
objNewPowerPoint.Visible = True
'Open your presentation and assign it to MyPresentation Object
Set MyPresentation = objNewPowerPoint.Presentations.Open(&amp;lt;Comeplete path&amp;gt;)
'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 &amp; &quot;: &quot; &amp; err.Description
End Sub

Important: Difference between Save and SaveAs

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 with a 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.

VBA Code to delete slide from PowerPoint Presentation

.Delete method is used to delete a single slide from Slide collection. This method belongs to a single Slide Object. You must identify a single slide item from Slides collection before deleting it.

Syntax:

pSlides(slideNumber).Delete

OR

pSlides.Item(slideNumber).Delete

Where:

pSlides : Variable of Slides type where Slide collection of the presentation stored from which slides has to be deleted

slideNumber : As the name says.. Slide number which you want to delete. Like for 1st Slide – 1, for second – 2 and so on..

Important Point

While adding a new slide, we add it in the collection (Slides), therefore .Add method belongs to Slides collection. But, while deleting a slide, we delete individual slides from the collection, therefore .Delete is a method of Slide object -> (Slides.Item(i))


Sub Delete_a_slide_from_presentation()
'Define one variable of Object Type to hold the Application Object
	Dim objNewPowerPoint As PowerPoint.Application
	Dim MyPresentation As Presentation
	Dim pSlides As Slides
	
	On Error GoTo err
'Create an Object for PowerPoint Application
	Set objNewPowerPoint = New PowerPoint.Application
	
'Make this Application Object Visible
	objNewPowerPoint.Visible = True
'Open your presentation and assign it to MyPresentation Object
	Set MyPresentation = objNewPowerPoint.Presentations.Open(&quot;PPT-Slides.pptx&quot;)
'Assign the Collection of Slide Objects to an Object Variable
	Set pSlides = MyPresentation.Slides
' Note: pSlides Object has all the slides in that presentation
' Below statement will delete the second slide from the Slides Collection
	pSlides.Item(2).Delete
'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 &amp; &quot;: &quot; &amp; err.Description
End Sub

About the Next Article…

In this article I will cover the following:
1. Create Power Point Presentations by creating the Object using CreateObject keyword.
2. Some Useful and practical Examples

FREE Download : In addition to this, I have created a simple yet powerful Excel VBA tool to consolidate Slides from different PowerPoint presentations in a particular folder.
I will not talk more about it here.

Buy a coffee for the author

Adsense

Download FREE Tools and Templates

There are many cool and useful excel tools and templates available to download for free. For most of the tools, you get the entire VBA code base too which you can look into it, play around it, and customize according to your need.

Convert PDF Tables to Excel with VBA | Step-by-Step Guide
Convert PDF Tables to Excel with VBA | Step-by-Step Guide

Convert PDF Tables to Excel with VBA | Step-by-Step Guide How to Convert PDF Tables to Excel Using Excel VBA (Macros) Extracting data from PDF tables into Excel can be tedious, but with Excel VBA (macros), you can automate this process and save time. This guide will...

Dynamic Arrays and Spill Functions in Excel: A Beginner’s Guide
Dynamic Arrays and Spill Functions in Excel: A Beginner’s Guide

In today's tutorial, we'll be diving into the exciting world of dynamic arrays and spill functions in Office 365 Excel. These features have revolutionized the way we work with data, providing a more flexible and efficient way to handle arrays. I am going to explain...

How to Declare a Public Variable in VBA
How to Declare a Public Variable in VBA

While programming in VBA sometimes you need to declare a Public Variable that can store the value throughout the program. Use of Public Variable: Let's say you have 4 different Functions in your VBA Code or Module and you have a variable that may or may not be...

What is Excel Formula?

Excel Formula is one of the best feature in Microsoft Excel, which makes Excel a very very rich application. There are so many useful built-in formulas available in Excel, which makes our work easier in Excel. For all the automated work, Excel Macro is not required. There are so many automated things can be done by using simple formulas in Excel. Formulas are simple text (With a Syntax) which is entered in to the Excel Worksheet Cells. So how computer will recognize whether it is a formula or simple text? Answer is simple.. every formula in Excel starts with Equal Sign (=).

You May Also Like…

4 Comments

  1. Mohammad Faizullah

    Hi,
    My name is Faiz. I need your help to consolidate multiple ppt slides in a single slide but condition is that multiple name in a excel sheet. how to consolidate according to Name wise(Name is available in excel sheet).plz help me to create this code.

    Thanks,
    Faiz

    Reply
    • Sai

      Hi Faiz,
      Have you got your answer ? If yes, reply with the code so that others can also learn from yours.

      Otherwise attach a sample file with your requirement.

      Reply
  2. vicky

    I am getting an run time error while opening ,save as and closing PowerPoint using excel vba. Need help its urgent

    thanks in advance

    Reply
  3. vicky

    I am getting an run time error while opening ,save as and closing PowerPoint using excel vba. Need help its urgent

    thanks in advance

    Reply

Trackbacks/Pingbacks

  1. Welcome to LearnExcelMacro.com Interacting with Powerpoint Slides – Tutorial – Part 2 (Last) - […] is a continuation of my previous article about Interaction with Power point Slides using Excel Macro. In my previous…
  2. Complete VBA tutorial to interact with Power point Presentations - Part - 2 - […] is a continuation of my previous article about Interaction with Power point Slides using Excel Macro. In my previous…

Submit a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Join and get a FREE! e-Book

Don't miss any articles, tools, tips and tricks, I publish here

You have Successfully Subscribed!

Pin It on Pinterest