Reputation: 535
I'm in Excel and I'd like to reference a custom layout for a slide in PowerPoint by name. You can only refer to them by index, so I thought a function should do the trick:
Sub Monatsbericht()
Dim DestinationPPT As String
Dim PowerPointApp As PowerPoint.Application
Dim myPresentation As PowerPoint.Presentation
Set PowerPointApp = New PowerPoint.Application
DestinationPPT = "C:\VBA\Reports\MonthlyReport_Template.pptm"
Set myPresentation = PowerPointApp.Presentations.Open(DestinationPPT)
Debug.Print PPLayout("CLayout1")
'Rest of code
End Sub
Function PPLayout(clayout As String)
Dim myPresentation As PowerPoint.Presentation
Dim olay As PowerPoint.CustomLayout
For Each olay In ActivePresentation.SlideMaster.CustomLayouts
If olay.Name = clayout Then
PPLayout = olay.Index
Exit Function
End If
Next olay
End Function
I get error 429: "Object creation by Activex component not possible.", highlighting the for each line in the function.
Upvotes: 1
Views: 875
Reputation: 57683
Actually ActivePresentation
should be myPresentation
, Excel should not know the ActivePresentation
. Also you must submit myPresentation
as a parameter otherwise this is an empty variable in your function.
If you have a look at the Slides.AddSlide method (PowerPoint) you see that the second parameter is not an index
but of type CustomLayout
so your function must return the layout instead of an index
.
Public Function PPLayout(clayout As String, myPresentation As PowerPoint.Presentation) As PowerPoint.CustomLayout
Dim olay As PowerPoint.CustomLayout
For Each olay In myPresentation.SlideMaster.CustomLayouts
If olay.Name = clayout Then
Set PPLayout = olay
Exit Function
End If
Next olay
End Function
And use it like
Debug.Print PPLayout("CLayout1", myPresentation).Index
or
myPresentation.Slides.AddSlide(myPresentation.Slides.Count + 1, PPLayout("CLayout1", myPresentation))
Upvotes: 2