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

如何使用vba在powerpoint中应用特定布局?,vba,powerpoint,Vba,Powerpoint,我在做一个项目。在这方面,我做了一个自定义主题,其中包括一个主幻灯片和可能的布局。 所以基本上我想对特定的幻灯片应用特定的布局。那么有没有办法通过编程来实现呢。 比如: activepresentation.Slides(1.Layout=“layoutname” 我知道上面的代码是错误的,但我希望像这样的东西来命名特定的布局。供您参考,我的版式名称是“没有客户标识的标题” 感谢ActivePresentation.Slides(1)。CustomLayout=ActivePresentatio

我在做一个项目。在这方面,我做了一个自定义主题,其中包括一个主幻灯片和可能的布局。 所以基本上我想对特定的幻灯片应用特定的布局。那么有没有办法通过编程来实现呢。 比如:

activepresentation.Slides(1.Layout=“layoutname”

我知道上面的代码是错误的,但我希望像这样的东西来命名特定的布局。供您参考,我的版式名称是“没有客户标识的标题”

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

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

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

如果需要使用该名称,请编写一个函数,在CustomLayouts集合中迭代,直到找到您要查找的名称并返回索引。

使用以下代码

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

谢谢

嘿,史蒂夫,实际上我解决了我的问题。你是对的,它需要一个函数。是我写的。谢谢你的评论。愿意分享你的功能吗,@PratikGujarathi?我知道这相当简单,但这会为这个问题的未来观众节省一些时间。