In a recent LinkedIn discussion, a question was asked by a VBA developer about two different methods for adding a new slide to a presentation using VBA within PowerPoint.
In this post, we explore the old Add method and the new AddSlide method and create a free wrapper function that you can use to take advantage of Microsoft’s IntelliSense feature within the VBE.
With the PowerPoint 2003 and earlier object model, the Add method was used:
' PowerPoint 2003 and earlier Public Sub AddSlide() Dim pptSlide As Slide Set pptSlide = ActivePresentation.Slides.Add (1, ppLayoutText) End Sub
This was very easy to use since IntelliSense showed you the list of possible layouts immediately after you typed the comma:
PowerPoint 2007, 2010, 2013
In the new Slide Master object model (from PowerPoint 2007), a new AddSlide method was introduced whereby you need to first create a reference to the slide’s CustomLayout:
' PowerPoint 2007 and later Public Sub AddSlide() Dim pptSlide As Slide Dim pptLayout As CustomLayout Set pptLayout = ActivePresentation.Slides(1).CustomLayout Set pptSlide = ActivePresentation.Slides.AddSlide(2, pptLayout) End Sub
The new method is not as convenient because IntelliSense does not show you the list of available Custom Layouts as you are typing.
The solution is to write a wrapper function that uses a custom variable enumeration as shown below. Now when you call our custom AddSlide function, IntelliSense shows you the list of Custom Layouts to choose from:
Option Explicit ' You can get the names of all the layouts using the developer function below Public Enum pptLayout myTitle = 1 myTitleContent = 2 mySectionHeader = 3 myTwoContent = 4 myComparison = 5 myTitleOnly = 6 myBlank = 7 myContentCaption = 8 myPictureCaption = 9 myTitleVerticalText = 10 myVerticalTitleText = 11 End Enum Public Sub AddSlide() Dim myNewSlide As Slide ' Create a new Section slide at the end of the presentation Set myNewSlide = GetNewSlide(mySectionHeader) ' Create a new Title and Content slide in position 2 Set myNewSlide = GetNewSlide(myTitleContent, 2) End Sub ' Wrapper function for PowerPoint 2007, 2010, 2013 ' to add and return a new slide at the end or defined place in the presentation ' Calling the function enables IntelliSense to use and display the custom pptLayout enumeration Public Function GetNewSlide(myLayout As pptLayout, Optional SlidePosition As Integer) As Slide Dim NewSlideIndex As Integer Dim NewSlideLayout As CustomLayout Select Case myLayout Case 1 To 11 Set NewSlideLayout = ActivePresentation.SlideMaster.CustomLayouts(myLayout) Case Else Set NewSlideLayout = ActivePresentation.SlideMaster.CustomLayouts(2) End Select ' Set the new slide index to either the value passed to the funtion or the last slide + 1 in the deck NewSlideIndex = IIf(SlidePosition = 0, ActivePresentation.Slides.Count + 1, SlidePosition) ' Create the new slide Set GetNewSlide = ActivePresentation.Slides.AddSlide(NewSlideIndex, NewSlideLayout) End Function
You can get the names of all of the Custom Layouts in the current presentation with the following function:
' Developer function to print the custom layout names in the immediate window (Ctl+G) ' Used to create the custom enumeration Public Function GetCustomLayoutNames() Dim pptLayout As CustomLayout For Each pptLayout In ActivePresentation.SlideMaster.CustomLayouts Debug.Print pptLayout.Index & " : " & pptLayout.Name Next End Function
Need Help with VBA Programming?
We provide an extensive range of VBA macro and add-in development services so why not contact us to discuss your needs?