VBA PowerPoint - 仅针对特定幻灯片限制宏

VBA PowerPoint - Restricting macro only for specific slide

我正在尝试对我的 PowerPoint 宏实施错误处理,将您限制为 运行 宏,除非您在幻灯片 5 上。我正在尝试使用命令:"Application.ActiveWindow.View <> 5 Then" 但它似乎无法识别我在幻灯片 5 上,正确的命令是什么?

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

activeSlide 不是 PowerPoint 对象并且您没有将其定义为任何其他对象,将其替换为 Sld 并添加 SlideIndex获取编号:

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