VBA不循环通过任何PowerPoint幻灯片

时间:2019-04-08 09:04:10

标签: excel vba powerpoint

我只是试图在Excel中使用以下VBA代码循环遍历PowerPoint幻灯片。

Sub test()

Dim slide As Object


For Each slide In ActivePresentation.Slides

    Debug.Print "test"

Next slide

End Sub

但是,我收到消息“运行时错误'424”。所需对象'。有人知道为什么ActivePresentation.Slides可能不起作用吗?我也尝试过Dim slide as Slide

我需要激活PowerPoint中的某些设置或参数吗?

任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:1)

VBA必须知道您所指的应用程序,才能使其遍历该应用程序中的对象。

1。打开VBA编辑器

2。在顶部功能区中,单击Tools> References,然后选中Microsoft PowerPoint X.0对象库框

现在您可以识别要引用的PowerPoint应用程序和演示文稿

Sub ppslides()

Dim pp As Object
Dim slide As Object
Dim PowerPoint As PowerPoint.Application
Set PowerPoint GetObject(, "PowerPoint.Application")


'Loops through each open PP presentation and puts the presentation name in a messagebox
For Each pp In PowerPoint.Presentations
    MsgBox pp.Name
Next pp

'These variables can be populated and used to refer to a specific Presentation in the upcoming loop
ppname = "Example"
ppindex = 1


'Loops through all slides in the presentation and puts their names in a messagebox
'REF should be replaced with a name, index, or one of the above variables
For each slide In PowerPoint.Presentations(REF).Slides
    MsgBox slide.Name
Next slide

End Sub

答案 1 :(得分:0)

尝试一下:

Sub test()

    Dim objPPTApp As Object
    Dim slide As Object

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

    For Each slide In objPPTApp.ActivePresentation.Slides

        Debug.Print "test"

    Next slide

End Sub