按名称从Excel中引用PowerPoint中的自定义布局

时间:2019-05-02 09:04:24

标签: excel vba powerpoint

我在Excel中,我想按名称引用PowerPoint中幻灯片的自定义布局。您只能按索引引用它们,所以我认为函数应该可以解决问题:

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

我收到错误429:“无法通过Activex组件创建对象。”,突出显示函数中的每一行。

1 个答案:

答案 0 :(得分:1)

实际上ActivePresentation应该是myPresentation,Excel应该不知道ActivePresentation。另外,您还必须提交myPresentation作为参数,否则该函数中的变量为空。

如果您查看Slides.AddSlide method (PowerPoint),则会看到第二个参数不是index,而是类型CustomLayout,因此您的函数必须返回布局而不是{{1 }}。

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

Debug.Print PPLayout("CLayout1", myPresentation).Index