Steffa91
Steffa91

Reputation: 17

VBA PowerPoint - Restricting macro only for specific slide

I am trying to implement a error handling to my PowerPoint macro that restricts you to run the macro unless you are on slide 5. I am trying to utilize the command: "Application.ActiveWindow.View <> 5 Then" but it does not seem to recognize I am on slide 5, what is the correct comand for it ?

Private Sub CommandButton1_Click()

    Dim Sld As Slide
    Dim Shp As Shape

    'ERROR HANDLING
        If ActivePresentation.Slides.Count < 5 Then
            MsgBox "You do not have any slides in your PowerPoint project."
            Exit Sub
        End If

    Set Sld = Application.ActiveWindow.View.Slide

        If activeSlide <> 5 Then
            MsgBox "You are not on the correct slide."
            Exit Sub
        End If

    Set Sld = Application.ActiveWindow.View.Slide
End Sub

Upvotes: 0

Views: 275

Answers (1)

John Korchok
John Korchok

Reputation: 4923

activeSlide is not a PowerPoint object and you haven't defined it as anything else, replace it with Sld and add SlideIndex to get the number:

Private Sub CommandButton1_Click()

    Dim Sld As Slide
    Dim Shp As Shape

    'ERROR HANDLING
        If ActivePresentation.Slides.Count < 5 Then
            MsgBox "You do not have any slides in your PowerPoint project."
            Exit Sub
        End If

    Set Sld = Application.ActiveWindow.View.Slide

        If Sld.SlideIndex <> 5 Then
            MsgBox "You are not on the correct slide."
            Exit Sub
        End If

End Sub

Upvotes: 3

Related Questions