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

时间:2016-04-12 14:27:43

标签: excel vba excel-vba powerpoint powerpoint-vba

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

  

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

这是我的代码:

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) 

不知道它有什么问题。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

HeJ小鼠, 我有同样的错误。 当您复制幻灯片时,它将成为SlideRange。您需要做的就是从以下范围中取出第一项:

    Dim spptRange As SlideRange
    Dim sppt As PowerPoint.Slide
    Set spptRange = PPApp.ActivePresentation.Slides(spptNr).Duplicate
    Set sppt = spptRange.Item(1)
相关问题