如何使用vba在powerpoint中应用特定布局?

Pra*_*thi 7 vba powerpoint-vba

我正在做一个项目.在那里,我制作了一个自定义主题,其中包括一个主幻灯片和可能的布局.所以基本上我想将特定的布局应用于特定的幻灯片.那么有没有办法通过编程方式来做到这一点.喜欢 :

activepresentation.Slides(1).Layout = "layoutname"

我知道上面的代码是错误的,但我希望这样的东西通过它的名称来调用特定的布局.为了您的信息,我的布局名称是"没有客户标识的标题".

谢谢

Ste*_*erg 8

ActivePresentation.Slides(1).CustomLayout = ActivePresentation.Designs(1).SlideMaster.CustomLayouts(x)

其中x是表示您的自定义布局的布局集合的索引。

与PPT OM中的大多数其他此类集合不同,该集合似乎无法接受索引或名称。它必须是一个索引。

如果需要使用该名称,请编写一个遍历CustomLayouts集合的函数,直到找到所要使用的名称并返回索引。


Ник*_*ков 5

使用以下代码

Sub ApplyLayoutByIndex()

    Dim sld As Slide
    Dim shp As Shape
    Dim xName As String
    Set sld = Application.ActiveWindow.View.Slide
    Dim xIndex As Integer

    xName = "A final slide"

    xIndex = getLayoutIndexByName(xName)

    If xIndex = 0 Then
    MsgBox "The layout name" & xName & "not found. Check the name of the layout", vbOKOnly
    Exit Sub
    End If

    sld.CustomLayout = ActivePresentation.Designs(1).SlideMaster.CustomLayouts(xIndex)

    End Sub

    Function getLayoutIndexByName(xName As String) As Integer
    ActivePresentation.Designs(1).SlideMaster.CustomLayouts.Item (1)
    With ActivePresentation.Designs(1).SlideMaster.CustomLayouts
        For i = 1 To .Count
            If .Item(i).Name = xName Then
            getLayoutIndexByName = i
            Exit Function
            End If
        Next
    End With

    End Function
Run Code Online (Sandbox Code Playgroud)

谢谢!