{"id":4238,"date":"2014-09-19T13:26:25","date_gmt":"2014-09-19T13:26:25","guid":{"rendered":"http:\/\/www.learnexcelmacro.com\/wp\/?p=4238"},"modified":"2022-08-12T11:42:47","modified_gmt":"2022-08-12T11:42:47","slug":"power-point-using-excel-vba-tutorial-part-1","status":"publish","type":"post","link":"https:\/\/vmlogger.com\/excel\/2014\/09\/power-point-using-excel-vba-tutorial-part-1\/","title":{"rendered":"Complete VBA tutorial to interact with Power point Presentations – Part – 1"},"content":{"rendered":"
[fusion_text]D<\/span><\/p>\n ear Friends,<\/p>\n 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 – <\/p>\n <\/i> VBA code to create a presentation slide based on a Table in Excel 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. Click on the links to directly jump to that particular section…<\/p>\n 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: <\/a><\/p>\n 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:<\/p>\n As soon as the above line of statements are executed you can see a Power Point File launched which will look like below:<\/p>\n 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.<\/p>\n Now we have an Application Object so we can create multiple Presentation File from this. <\/p>\n Above code snippet will add 2 presentations (2 PowerPoint files) as you can see in the below images:<\/p>\n But these presentations will be looking something like below – an Empty Presentations without any Slide in it.<\/p>\n Power Point Presentation Object<\/p><\/div>\n 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. 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<\/strong> using .Add<\/strong> method of Slides object (Collection of Slide). .Add method belongs to Slides<\/strong> Object. Index Number: <\/strong> Layout of the slide: <\/strong> <\/a><\/p>\n Let’s have a look – on how to save and close a power point presentation using Excel VBA. In case you want to save your presentation with a different name or at different location then you can use .SaveAs<\/strong> method. The only difference in both these methods is – For .Save<\/strong> you do not need to provide file path but for .SaveAs<\/strong> it is mandatory to provide the file path. Refer below code.<\/p>\n 1. How to create Power Point Application , Presentations and Slides Objects <\/a><\/p>\n 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 :<\/p>\n 1. Create 2 New Power Point 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. <\/p>\n To open a presentation you can use the below statement:<\/p>\n objNewPowerPoint –<\/strong> is your defined Power Point Application Object 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.<\/p>\n Now you can use MyPresentation3<\/strong> presentation object to add slides to it, delete, save, close etc. same as the above code.<\/p>\n Now let us create an example with everything we learnt so far. In this example, I am going to do the following:<\/p>\n 1. open an Existing power point presentationSample List of Questions…<\/h2>\n
\n<\/i> Excel macro to create a Slide with Graph and Table in Excel
\n<\/i> Macro to paste Graph from Excel in to a PPT in a specific Slide
\n<\/i> VBA code to remove old graph from a specific slide and place the new generated graph in Excel – Like refresh button<\/p>\n<\/div>\n<\/div>\n
\nFurthermore, 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<\/em> and so on..
\nFinally, at the end of this tutorial<\/strong>, you will find a FREE Excel VBA tool to download<\/em><\/strong>. Mostly in the next article of this tutorial. <\/p>\nTopics covered in this Article<\/h1>\n
<\/i> Basics about Power Point Application Object Model in Excel VBA<\/h2>\n
<\/i> VBA Code to create a new Presentation (Power Point Presentation File)<\/h2>\n
<\/i> VBA Code to add slides in PPT<\/em><\/h2>\n
<\/i> VBA Code to save a New PPT – SaveAs statement<\/em><\/h2>\n
<\/i> VBA Code to open an existing presentation file<\/h2>\n
<\/i> VBA Code to save an existing presentation file – Save statement<\/em><\/h2>\n
<\/i> VBA Code to Save and Close powerpoint presentation<\/em><\/h2>\n
<\/i> VBA Code to delete slides in PPT<\/em><\/h2>\n<\/div>\n<\/div>\n
\nhttp:\/\/msdn.microsoft.com\/en-us\/library\/office\/aa213568(v=office.11).aspx<\/a>.
\n <\/p>\nVBA to Create a New Power Point Application<\/h1>\n
\r\nSub Create_PowerPoint_Object\r\n'Define one variable of Object Type to hold the Application Object\r\nDim objNewPowerPoint as Object\r\n'Create an Object for PowerPoint Application\r\nSet objNewPowerPoint = CreateObject("PowerPoint.Application")\r\nobjNewPowerPoint.Visible = True\r\nEnd Sub\r\n<\/code><\/pre>\n
\n
\r\nsub Add_Two_Presentations\r\n'Define one variable of Object Type to hold the Application Object\r\nDim objNewPowerPoint As Object\r\nDim MyPresentation1 As Object\r\nDim MyPresentation2 As Object\r\n'Create an Object for PowerPoint Application\r\nSet objNewPowerPoint = CreateObject("PowerPoint.Application")\r\n'Make this Application Object Visible\r\nobjNewPowerPoint.Visible = True\r\n'Create 2 presentations from that Application Object\r\nSet MyPresentation1 = objNewPowerPoint.Presentations.Add\r\nSet MyPresentation2 = objNewPowerPoint.Presentations.Add\r\nEnd Sub\r\n<\/code><\/pre>\n
<\/p>\n
\n<\/a><\/p>\nVBA code to add slides to PowerPoint Presentation<\/h1>\n
\nRefer the Syntax of this method below<\/p>\nSyntax of .Add Method<\/h2>\n
\n[highlight color=”yellow”]MyPresentation1.Slides.Add <Index Number> , <Layout of the slide>[\/highlight]<\/p>\nWhere:<\/h2>\n
\nSlides<\/i> 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.
\nFor example:<\/strong> 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.<\/p>\n
\nThis 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)<\/p>\n\r\nSub Add_slide_to_PowerPoint()\r\n'Define one variable of Object Type to hold the Application Object\r\n\tDim objNewPowerPoint As PowerPoint.Application\r\n\tDim MyPresentation As Presentation\r\n\tDim pSlides As Slides\r\n\tOn Error GoTo err\r\n'Create an Object for PowerPoint Application\r\n\tSet objNewPowerPoint = New PowerPoint.Application\r\n\t\r\n'Make this Application Object Visible\r\n\tobjNewPowerPoint.Visible = True\r\n'Open your presentation and assign it to MyPresentation Object\r\n\tSet MyPresentation = objNewPowerPoint.Presentations.Open("C:\\Users\\vmishra\\Desktop\\PPT-Slides.pptx")\r\n'Assign the Collection of Slide Objects to an Object Variable\r\n\tSet pSlides = MyPresentation.Slides\r\n' Note: pSlides Object has all the slides in that presentation\r\n' Below statement will add a new slide to the collection\r\n' at the given location, e.g. at 3rd position.\r\n' SlideLayout is given as Blank slide layout\r\n\tpSlides.Add 3, ppLayoutBlank\r\n'save and close the presentation\r\n\tMyPresentation.Save\r\n\tMyPresentation.Close\r\n'release the memory from all the objects\r\n\tSet MyPresentation = Nothing\r\n\tSet pSlides = Nothing\r\n'Finally quit the Power Point application\r\n\tobjNewPowerPoint.Quit\r\n\tExit Sub\r\n\terr:\r\n'if any Error occurred, display the error code and description\r\n\tMsgBox err.Number & ": " & err.Description\r\nEnd Sub\r\n<\/code><\/pre>\n
Save & Close PowerPoint Through Excel VBA<\/h1>\n
\n.Save<\/strong> and .Close<\/strong> is the keywords to save and close a presentation respectively. These methods belongs to Presentation Object.<\/p>\n\r\n\tDim objNewPowerPoint As PowerPoint.Application\r\n\tDim MyPresentation As Presentation\r\n\t\r\n\tSet objNewPowerPoint = New PowerPoint.Application\r\n\t'Open your presentation and assign it to MyPresentation Object\r\n\tSet MyPresentation = objNewPowerPoint.Presentations.Open("C:\\Users\\...\\Desktop\\PPT-Slides.pptx")\r\n\t'Save the presentation \r\n\tMyPresentation.Save\r\n\t\r\n\t'Close the presentation\r\n\tMyPresentation.Close\r\n<\/code><\/pre>\n
\r\n\tDim objNewPowerPoint As PowerPoint.Application\r\n\tDim MyPresentation As Presentation\r\n\t\r\n\tSet objNewPowerPoint = New PowerPoint.Application\r\n\t'Open your presentation and assign it to MyPresentation Object\r\n\tSet MyPresentation = objNewPowerPoint.Presentations.Open("C:\\Users\\...\\Desktop\\PPT-Slides.pptx")\r\n\t\r\n\t'SaveAs the presentation at different location with different name\r\n\tMyPresentation.SaveAs Filename:=desktopURL & "C:\\..\\..\\Desktop\\PPT-Slides-New.pptx"\r\n<\/code><\/pre>\n
What did we learn so far?<\/h2>\n
\n2. How to create new Power Point Presentations
\n3. How to add slides to power point presentation
\n4. How to Save and Close Presentations in Excel VBA\n<\/p><\/div>\nVBA to Create a New Power Point Presentation<\/h1>\n
\n2. Add 5 slides in one presentation and 3 in second one.
\n3. Save these power point presentation files on your desktop<\/p>\n\r\nOption Explicit\r\nSub Add_Two_Presentations()\r\n'Define one variable of Object Type to hold the Application Object\r\nDim objNewPowerPoint As Object\r\nDim MyPresentation1 As Object\r\nDim MyPresentation2 As Object\r\nDim p1Slides As Object\r\nDim p2Slides As Object\r\nDim iSlide As Integer\r\nDim desktopURL\r\nOn Error GoTo err\r\n'Create an Object for PowerPoint Application\r\nSet objNewPowerPoint = CreateObject("PowerPoint.Application")\r\n'Make this Application Object Visible\r\nobjNewPowerPoint.Visible = True\r\n'Create 2 presentations from that Application Object\r\nSet MyPresentation1 = objNewPowerPoint.Presentations.Add\r\nSet MyPresentation2 = objNewPowerPoint.Presentations.Add\r\n'Assign the Collection of Slide Objects to an Object Variable\r\nSet p1Slides = MyPresentation1.Slides\r\nSet p2Slides = MyPresentation2.Slides\r\n\r\n'Now add 5 slides in first presentation and 3 in second one\r\nFor iSlide = 1 To 5\r\n p1Slides.Add iSlide, ppLayoutCustom\r\n 'this will add only 3 slides to the second ppt\r\n If iSlide <= 3 Then\r\n p2Slides.Add iSlide, ppLayoutCustom\r\n End If\r\nNext\r\n\r\n'get the desktop path\r\ndesktopURL = CreateObject("WScript.Shell").specialfolders("Desktop")\r\n'save both the ppts on the desktop\r\nMyPresentation1.SaveAs Filename:=desktopURL & "\\ppt1.pptx"\r\nMyPresentation2.SaveAs Filename:=desktopURL & "\\ppt2.pptx"\r\n\r\n'close both the presentations\r\nMyPresentation1.Close\r\nMyPresentation2.Close\r\n\r\n'release the memory from all the objects\r\nSet MyPresentation1 = Nothing\r\nSet MyPresentation2 = Nothing\r\nSet p1Slides = Nothing\r\nSet p2Slides = Nothing\r\n'Finally quit the Power Point application\r\nobjNewPowerPoint.Quit\r\nExit Sub\r\nerr:\r\n'if any Error occurred, display the error code and description\r\nMsgBox err.Number & ": " & err.Description\r\nEnd Sub\r\n<\/code><\/pre>\n
\nYou 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
\n<\/a><\/p>\nExcel VBA to open PowerPoint Presentation File<\/h1>\n
\r\nobjNewPowerPoint.Presentations.Open("filepath")\r\n<\/code><\/pre>\n
Where :<\/h2>\n
\nFilePath – <\/strong>This is the complete path of the file with file name with file extension<\/p>\n\r\nDim MyPresentation3 as Object\r\nSet MyPresentation3 = objNewPowerPoint.Presentations.Open("filepath")\r\n<\/code><\/pre>\n
Example:<\/h1>\n
\n2. 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)
\n3. Save and close the presentation<\/p>\n<\/div>\n\r\n\r\nSub Open_an_existing_Presentations()\r\n'Define one variable of Object Type to hold the Application Object\r\nDim objNewPowerPoint As Object\r\nDim MyPresentation As Object\r\nDim pSlides As Object\r\n\r\nOn Error GoTo err\r\n'Create an Object for PowerPoint Application\r\nSet objNewPowerPoint = CreateObject("PowerPoint.Application")\r\n'Make this Application Object Visible\r\nobjNewPowerPoint.Visible = True\r\n'Open your presentation and assign it to MyPresentation Object\r\nSet MyPresentation = objNewPowerPoint.Presentations.Open(&lt;Comeplete path&gt;)\r\n'Assign the Collection of Slide Objects to an Object Variable\r\nSet pSlides = MyPresentation.Slides\r\n'Note: pSlides Object has all the slides in that presentation\r\n'Now add a new slide at 3rd position in the collection of slides pSlides\r\npSlides.Add 3, ppLayoutCustom\r\n'save and close the presentation\r\nMyPresentation.Save\r\nMyPresentation.Close\r\n'release the memory from all the objects\r\nSet MyPresentation = Nothing\r\nSet pSlides = Nothing\r\n'Finally quit the Power Point application\r\nobjNewPowerPoint.Quit\r\nExit Sub\r\nerr:\r\n'if any Error occurred, display the error code and description\r\nMsgBox err.Number & ": " & err.Description\r\nEnd Sub\r\n<\/code><\/pre>\n