无法从Excel VBA添加新的ppt幻灯片,错误429

问题描述 投票:0回答:2

我正在研究一个主题,我需要将Excel数据导出到PowerPoint中。一切都很好,直到我只有一张幻灯片。但是当我尝试添加另一张幻灯片时,它显示错误:

运行时错误429:Activex组件无法创建对象。

这是我的代码:

Dim pptSlide As Slide 
Dim pptLayout As CustomLayout 
'my code
Set pptLayout = ActivePresentation.Slides(1).CustomLayout 'error at this line
Set pptSlide = ActivePresentation.Slides.AddSlide(2, pptLayout) 

不知道它有什么问题。

excel vba excel-vba powerpoint powerpoint-vba
2个回答
0
投票

当代码中有一张幻灯片时,该代码对我来说没问题,但当甲板上没有幻灯片时它会失败,因为它引用了幻灯片1以获得对其自定义布局的引用。

Dim pptSlide As Slide
Dim pptLayout As CustomLayout
'my code
With ActivePresentation
  ' If the deck has some slides, get a freference to the first slide's custom layout
  If .Slides.Count > 0 Then
    Set pptLayout = .Slides(1).CustomLayout 'error at this line
  Else
    ' If no slides in the deck, use the second custom layout from the master
    ' (usually the Title and Content layout
    Set pptLayout = .SlideMaster.Design.SlideMaster.CustomLayouts(2)
  End If
  Set pptSlide = .Slides.AddSlide(.Slides.Count + 1, pptLayout)
End With

0
投票

嘿,我有同样的错误。当您复制幻灯片时,它将成为SlideRange。您需要做的就是从范围中获取第一项,如下所示:

    Dim spptRange As SlideRange
    Dim sppt As PowerPoint.Slide
    Set spptRange = PPApp.ActivePresentation.Slides(spptNr).Duplicate
    Set sppt = spptRange.Item(1)
© www.soinside.com 2019 - 2024. All rights reserved.