A couple days ago, a reader asked about there is a way to programmatically create PowerPoint presentations from scratch and add slides to an existing presentation.
As we indicated in the past, PowerPoint doesn’t ship a Macro Recorder (Excel and Word has built in recorders for Macro). Therefore, we are left with good old Visual Basic for Application that we can use to create simple Macros to automate PowerPoint. There are further alternatives for Powerpoint Automation, but those goes beyond the scope of this tutorial.
In today’s short tutorial, we’ll go over two use cases:
Let’s get started.
Look at the Ribbon. If you don’t see the a Developer entry in the menu, before start your coding you’ll need to enable the developer tab.
Next, hit on Developer, then hit on the Visual Basic button (or hit Alt+f11) to open the Visual Basic IDE.
Sub CreatePresentation() Dim NewPres as Presentation Set NewPres = Presentations.Add NewPres.SaveAs("MyPresentation.pptx") End Sub
Sub CreateSlide() Dim NewSlide as Slide 'This statement ads a title slide Set NewSlide = ActivePresentation.Slides.Add(Index:=1, Layout:=ppLayoutTitle) 'This statement adds a blank slide in the second place Set NewSlide = ActivePresentation.Slides.Add(Index:=2, Layout:=ppLayoutBlank) End Sub