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
Click on the links to directly jump to that particular section…
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:
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:
But these presentations will be looking something like below – an Empty Presentations without any Slide in it.
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("C:\Users\vmishra\Desktop\PPT-Slides.pptx")
'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 & ": " & 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("C:\Users\...\Desktop\PPT-Slides.pptx")
'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("C:\Users\...\Desktop\PPT-Slides.pptx")
'SaveAs the presentation at different location with different name
MyPresentation.SaveAs Filename:=desktopURL & "C:\..\..\Desktop\PPT-Slides-New.pptx"
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("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 <= 3 Then
p2Slides.Add iSlide, ppLayoutCustom
End If
Next
'get the desktop path
desktopURL = CreateObject("WScript.Shell").specialfolders("Desktop")
'save both the ppts on the desktop
MyPresentation1.SaveAs Filename:=desktopURL & "\ppt1.pptx"
MyPresentation2.SaveAs Filename:=desktopURL & "\ppt2.pptx"
'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 & ": " & 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("filepath")
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("filepath")
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("PowerPoint.Application")
'Make this Application Object Visible
objNewPowerPoint.Visible = True
'Open your presentation and assign it to MyPresentation Object
Set MyPresentation = objNewPowerPoint.Presentations.Open(&lt;Comeplete path&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 & ": " & 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("PPT-Slides.pptx")
'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 & ": " & 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.
没有评论:
发表评论