首页 文章

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

提问于
浏览
6

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

longSlideCount = ActivePresentation.Slides.Count

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

...插入一个'ppLayoutTitle'类型的新幻灯片,但我想知道是否可以在'Slide Master View'中创建自定义布局,然后将 that 特定幻灯片模板插入到演示文稿中?

提前致谢!!!

1 回答

  • 11

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

    一旦引用了所需的 CustomLayout 对象,就可以使用 Slides 集合的AddSlide method,它将 CustomLayout 对象作为第二个参数(与您在问题中使用的 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
    

相关问题