VBA没有循环任何PowerPoint幻灯片

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

我只是尝试使用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中的一些设置或参数吗?

任何帮助赞赏。

excel vba powerpoint
2个回答
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

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
© www.soinside.com 2019 - 2024. All rights reserved.