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

丶灬走出姿态 提交于 2019-12-03 15:58:59

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!