How to apply particular layout in powerpoint using vba?

血红的双手。 提交于 2019-12-17 16:28:54

问题


I am working on one project. In that I made one custom theme which includes one master slide and may layouts. so basically i want to apply particular layout to specific slides. So is there any way to do it by programmatically. like :

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

I know above code is wrong but i want something like this to call particular layout by its name. for your information my layout name is "Title without Client Logo".

Thanks


回答1:


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

where x is the index into the layouts collection that represents your custom layout.

Unlike most other such collections in the PPT OM, this one seems unable to accept either an index or a name. It must be an index.

If you need to work with the name, write a function that iterates through the CustomLayouts collection until it finds the name you're after and returns the index.




回答2:


Use the following code

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

Thanks!



来源:https://stackoverflow.com/questions/9147643/how-to-apply-particular-layout-in-powerpoint-using-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!