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

时间:2014-01-08 13:49:58

标签: vba powerpoint powerpoint-vba powerpoint-2010

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

longSlideCount = ActivePresentation.Slides.Count

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

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

提前致谢!!!

1 个答案:

答案 0 :(得分:11)

可以通过VBA通过Presentation对象CustomLayoutscollectionSlideMaster 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

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

{{1}}