简体   繁体   English

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

[英]VBA not looping through any PowerPoint slides

I am simply trying to loop through PowerPoint slides using the following VBA code in Excel. 我只是想在Excel中使用以下VBA代码循环浏览PowerPoint幻灯片。

Sub test()

Dim slide As Object


For Each slide In ActivePresentation.Slides

    Debug.Print "test"

Next slide

End Sub

However I receive the message 'Runtime error '424'. 但是,我收到消息“运行时错误'424”。 Object required'. 所需对象'。 Anybody have any idea why ActivePresentation.Slides may not be functioning? 有人知道为什么ActivePresentation.Slides可能不起作用吗? I have tried to Dim slide as Slide also. 我也尝试将Dim slide as Slide

Is there some setting or parameter in PowerPoint I need to activate? 我需要激活PowerPoint中的某些设置或参数吗?

Any help appreciated. 任何帮助表示赞赏。

VBA has to know what application you are referring to, in order for it to loop through objects within that application. VBA必须知道您所指的是哪个应用程序,才能使其遍历该应用程序内的对象。

1. Open the VBA editor 1.打开VBA编辑器

2. In the top ribbon, click on Tools > References , and check the box for Microsoft PowerPoint X.0 Object Library 2.在顶部功能区中,单击“ Tools >“ References ,然后选中“ Microsoft PowerPoint X.0对象库”框。

Now you can identify the PowerPoint application and Presentation you want to refer to 现在,您可以识别要引用的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

Try this: 尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM