Ciaran De Ceol
Ciaran De Ceol

Reputation: 35

VBA not looping through any PowerPoint slides

I am simply trying to loop through PowerPoint slides using the following VBA code in Excel.

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'. Object required'. Anybody have any idea why ActivePresentation.Slides may not be functioning? I have tried to Dim slide as Slide also.

Is there some setting or parameter in PowerPoint I need to activate?

Any help appreciated.

Upvotes: 0

Views: 1155

Answers (2)

Tim Stack
Tim Stack

Reputation: 3248

VBA has to know what application you are referring to, in order for it to loop through objects within that application.

1. Open the VBA editor

2. In the top ribbon, click on Tools > References, and check the box for Microsoft PowerPoint X.0 Object Library

Now you can identify the PowerPoint application and Presentation you want to refer to

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

Upvotes: 1

CLR
CLR

Reputation: 12279

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

Upvotes: 0

Related Questions