6

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

longSlideCount = ActivePresentation.Slides.Count

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

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

提前致谢!!!

4

1 回答 1

14

您的所有自定义布局都可以通过 VBA 通过对象属性CustomLayouts集合进行访问。创建自定义布局时,请为其指定一个有意义的名称。然后你可以从集合中获取它。Microsoft 似乎没有实现按名称查找,因此您必须遍历集合才能找到具有正确名称的对象。SlideMasterPresentationCustomLayoutsCustomLayout

一旦您获得了对所需CustomLayout对象的引用,您就可以使用集合的AddSlide方法,该方法将对象作为第二个参数(与您在问题中使用的,并且采用枚举值相反)。SlidesCustomLayoutSlides.AddPpSlideLayout

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

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
于 2014-01-08T14:36:05.183 回答