Share via

Apply specific layout from custom template with VBA code

Anonymous
2012-11-27T20:17:11+00:00

Hello,

I have some VBA code in Excel to create a new Powerpoint presentation with multiple charts and tables. I'm using a company standard Powerpoint template, and I'm having problems selecting the layout that I want.

When I add

Sub CreatePowerPoint()

Dim TempPath As String

On Error Resume Next

Set PPApp = GetObject(, "PowerPoint.Application")

On Error GoTo 0

PPApp.Presentations.Add

PPApp.Visible = True

PPApp.ActiveWindow.ViewType = ppViewSlide

Set PPPres = PPApp.ActivePresentation

TempPath = Application.TemplatesPath & "Company_Standard_template_16x9.potx"

PPPres.ApplyTemplate TempPath

PPPres.Slides.Add PPPres.Slides.Count + 1, ppLayoutCustom

In this last line, I can only select the standard layouts from Powerpoint (starting by "ppLayout..."). When I use the "ppLayoutCustom", it selects the first custom layout in the template, but there are several others. How can I refer to a different custom layout than the first one?

Thanks!!

Jose

Microsoft 365 and Office | PowerPoint | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Steve Rindsberg 99,161 Reputation points MVP Volunteer Moderator
2012-11-27T22:06:01+00:00

Here's an example that will help.  You need to specify the Design (ie, the Master), which will be 1 unless there are multiple masters, and you need to specify the INDEX of the custom layout within that design.  If you know the index number and are sure it won't change, then the first example below will be simplest.

If you know the custom layout's name, you can call the GetLayoutIndexFromName function, as I've done in the second example.

Sub Test()

With ActivePresentation

' get the layout by index:

.Slides.AddSlide .Slides.Count + 1, .Designs(1).SlideMaster.CustomLayouts(13)

' or get the layout by name:

.Slides.AddSlide .Slides.Count + 1, .Designs(1).SlideMaster.CustomLayouts( _

GetLayoutIndexFromName("MyLayout1", .Designs(1)))

End With

End Sub

Function GetLayoutIndexFromName(sLayoutName As String, oDes As Design) As Long

Dim x As Long

For x = 1 To oDes.SlideMaster.CustomLayouts.Count

If oDes.SlideMaster.CustomLayouts(x).Name = sLayoutName Then

GetLayoutIndexFromName = x

Exit Function

End If

Next

End Function

Was this answer helpful?

6 people found this answer helpful.
0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Anonymous
    2013-07-11T21:26:29+00:00

    I found a simplest way, but using always the Collection index of the custom layout:

    ActiveWindow.Selection.SlideRange.CustomLayout = ActivePresentation.Designs(1).SlideMaster.CustomLayouts(7)

    This will change the layout of the current slide using the 7 layout of the first Design.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  2. Anonymous
    2012-11-28T17:44:33+00:00

    Worked like a charm!  Thanks so much, Steve!!

    Was this answer helpful?

    0 comments No comments