使用母版在VBA中为PowerPoint 2010创建具有自定义布局的新幻灯片 [英] Create a new slide in VBA for PowerPoint 2010 with custom layout using Master

查看:381
本文介绍了使用母版在VBA中为PowerPoint 2010创建具有自定义布局的新幻灯片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  longSlideCount = ActivePresentation.Slides.Count 

具有ActivePresentation.Slides
设置slideObject = .Add(longSlideCount + 1,ppLayoutTitle)

结尾

...这会插入类型为 ppLayoutTitle的新幻灯片,但我想知道是否可以在幻灯片主视图中创建自定义布局,然后插入

预先感谢!

解决方案方案

所有自定义布局都可以通过 CustomLayouts 集合 library / office / ff745484(v = office.14).aspx rel = noreferrer> 演示文稿 SlideMaster 属性 / code> obj等等。创建自定义布局时,请为其赋予一个有意义的名称。然后,您可以从 CustomLayouts 集合中获取它。似乎Microsoft并未按名称实现查找,因此您将不得不遍历该集合以查找具有正确名称的 CustomLayout 对象。



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



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

 公共函数GetLayout(_ 
LayoutName作为字符串,_
可选ParentPresentation作为表示= Nothing)作为CustomLayout

如果ParentPresentation没有然后
设置ParentPresentation = ActivePresentation
如果

Dim oLayout作为CustomLayout
对于ParentPresentation.SlideMaster.CustomLayouts中的每个oLayout
如果oLayout.Name = LayoutName然后
设置GetLayout = oLayout
退出
结束如果
下一个
结束函数

Sub AddCustomSlide()
Dim oSlides作为幻灯片,作为oSlide作为幻灯片
设置oSlides = ActivePresentation.Slides
设置oSlide = oSlides.AddSlide(oSlides.Count + 1,GetLayout( Smiley))
结束子


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

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 that particular slide template into the presentation?

Thanks in advance!!!

解决方案

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

Once you have a reference to the desired CustomLayout object, you use the AddSlide method of the Slides collection, which takes a CustomLayout object as the second arguments (as opposed to Slides.Add, which you used in your question, and which takes a PpSlideLayout enumeration value).

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

这篇关于使用母版在VBA中为PowerPoint 2010创建具有自定义布局的新幻灯片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆