10

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

Pratik Gujarathi
  • 748
  • 10
  • 22
  • 40

3 Answers3

11

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.

Steve Rindsberg
  • 3,470
  • 1
  • 16
  • 10
  • hey steve, actually i solved my problem. ya you are right function is needed for it. And i wrote it. thanks for your comment. – Pratik Gujarathi Feb 05 '12 at 19:08
  • Care to share your function, @PratikGujarathi ? I know it's fairly simple, but it would save future viewers of this question some time. – sfarbota Feb 21 '15 at 19:51
8

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!

NKAT
  • 160
  • 3
  • 10
0

Here is what I came up with, without using index, and consider the cases of more than 1 designs:

    Dim design As design
    Dim layout As CustomLayout
    Dim newLayoutName As String
    Dim designName As String
    
    ' design name if you want
    designName = "Some-Design-Name"
    ' layout name to be set
    newLayoutName = "Your-custom-layout-name"
    
    ' find the layout by looping through all designs / layouts
    For Each design In ActivePresentation.Designs
        ' if found
        If InStr(1, design.Name, designName) > 0 Then
            Debug.Print design.Name
            ' loop through all layouts
            For Each layout In design.SlideMaster.CustomLayouts
                ' if found
                If InStr(1, layout.Name, newLayoutName, vbTextCompare) > 0 Then
                    Debug.Print "Found:", layout.Name
                    ' apply new layout
                    ActiveWindow.View.Slide.CustomLayout = layout
                    ' quit
                    Exit For
                End If
            Next layout
            ' quit
            Exit For
        End If
    Next design
nphaibk
  • 71
  • 7