使用Master通过自定义布局在VBA for PowerPoint 2010中创建新幻灯片

Hot*_*non 6 powerpoint vba powerpoint-vba powerpoint-2010

我有以下VBA代码来创建一个新的PowerPoint幻灯片:

longSlideCount = ActivePresentation.Slides.Count

With ActivePresentation.Slides
    Set slideObject = .Add(longSlideCount + 1, ppLayoutTitle)
End With
Run Code Online (Sandbox Code Playgroud)

...插入'ppLayoutTitle'类型的新幻灯片,但我想知道是否可以在'幻灯片母版视图'中创建自定义布局,然后将该特定幻灯片模板插入到演示文稿中?

提前致谢!!!

Jos*_*nig 12

您所有的自定义布局可以通过VBA通过访问CustomLayouts收集的的SlideMaster财产一个的Presentation对象.创建自定义布局时,请为其指定有意义的名称.然后你可以从CustomLayouts集合中获取它.看起来Microsoft没有按名称实现查找,因此您必须遍历集合才能找到CustomLayout具有正确名称的对象.

一旦引用了所需的CustomLayout对象,就可以使用集合的AddSlide方法,该方法SlidesCustomLayout对象作为第二个参数(与Slides.Add您在问题中使用的对象相对应,并采用PpSlideLayout枚举值).

下面是一个帮助方法,用于按名称获取自定义布局,以及根据需要使用它的示例:

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
Run Code Online (Sandbox Code Playgroud)