0

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
1
  • 2
    You already captured Sld. Could you not say something like If Sld.Number <> 5 Then? Also, the second Set Sld is not needed. Commented Jan 13, 2020 at 15:31

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, it was exactly what I was looking for !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.