Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a new slide in VBA for PowerPoint 2010 with custom layout using Master

I have the following VBA code to create a new PowerPoint slide:

longSlideCount = ActivePresentation.Slides.Count

With ActivePresentation.Slides
    Set slideObject = .Add(longSlideCount + 1, ppLayoutTitle)
End With

...which inserts a new slide of type 'ppLayoutTitle', but I am wondering if it is possible to create a custom layout in the 'Slide Master View' and then insert that particular slide template into the presentation?

Thanks in advance!!!

like image 848
HotDogCannon Avatar asked Jan 08 '14 13:01

HotDogCannon


People also ask

How do you create a master slide in PowerPoint 2010?

Select the View tab, then click the Slide Master command. The presentation will switch to Slide Master view, and the Slide Master tab will be selected on the Ribbon. In the left navigation pane, scroll up and select the first slide. This is the slide master.

How do I create a custom slide master in PowerPoint?

Add an additional slide master to a presentation Click VIEW > Slide Master. On the SLIDE MASTER tab, do one of the following: In the Edit Theme group, click Themes, and then under Built-in, select a theme that you want the additional slide master to adhere to. In the Edit Master group, click Insert Slide Master.

How do I add a slide in PowerPoint VBA?

VBA to insert slides In the VB Editor, right click on VBAProject and hit Insert.


1 Answers

All your custom layouts can be accessed via VBA through the CustomLayoutscollection of the SlideMaster property of a Presentation object. When you create a custom layout, give it a meaningful name. Then you can fetch it from the CustomLayouts collection. It appears that Microsoft didn't implement lookup by name, so you will have to iterate through the collection to find the CustomLayout object with the right name.

Once you have a reference to the desired CustomLayout object, you use the AddSlide method of the Slides collection, which takes a CustomLayout object as the second arguments (as opposed to Slides.Add, which you used in your question, and which takes a PpSlideLayout enumeration value).

Below is a helper method for fetching a custom layout by name, and example of using it as you wanted:

Public Function GetLayout( _
    LayoutName As String, _
    Optional ParentPresentation As Presentation = Nothing) As CustomLayout

    If ParentPresentation Is Nothing Then
        Set ParentPresentation = ActivePresentation
    End If

    Dim oLayout As CustomLayout
    For Each oLayout In ParentPresentation.SlideMaster.CustomLayouts
        If oLayout.Name = LayoutName Then
            Set GetLayout = oLayout
            Exit For
        End If
    Next
End Function

Sub AddCustomSlide()
    Dim oSlides As Slides, oSlide As Slide
    Set oSlides = ActivePresentation.Slides
    Set oSlide = oSlides.AddSlide(oSlides.Count + 1, GetLayout("Smiley"))
End Sub
like image 103
Joshua Honig Avatar answered Nov 08 '22 17:11

Joshua Honig