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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20997571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:31:37  来源:igfitidea点击:

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

vbapowerpointpowerpoint-vbapowerpoint-2010

提问by HotDogCannon

I have the following VBA code to create a new PowerPoint slide:

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

longSlideCount = ActivePresentation.Slides.Count

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

...which inserts a new slide of type 'ppLayoutTitle', but I am wondering if it is possible to create a custom layout in the 'Slide Master View' and then insert thatparticular slide template into the presentation?

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

Thanks in advance!!!

提前致谢!!!

回答by Joshua Honig

All your custom layouts can be accessed via VBA through the CustomLayoutscollectionof the SlideMasterpropertyof a Presentationobject. When you create a custom layout, give it a meaningful name. Then you can fetch it from the CustomLayoutscollection. It appears that Microsoft didn't implement lookup by name, so you will have to iterate through the collection to find the CustomLayoutobject with the right name.

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

Once you have a reference to the desired CustomLayoutobject, you use the AddSlidemethodof the Slidescollection, which takes a CustomLayoutobject as the second arguments (as opposed to Slides.Add, which you used in your question, and which takes a PpSlideLayoutenumeration value).

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

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