打开 PowerPoint 幻灯片时如何 运行 VBA 编码

How can I run VBA code when a PowerPoint slide opens

我正在使用 PowerPoint 2016。

我在此论坛上发现了其他问题(如 here),表明答案是使用 OnSlideShowPageChange 或 slideshownextslide 事件。然而,在我看来,这些事件并没有发生。

我的演示文稿中的模块中有以下代码

Public Sub OnSlideShowPageChange(ByVal Wn As SlideShowWindow)

    Dim i As Integer
    Dim sld As Slide
    Dim shp As Shape
    Dim boxText As String

     MsgBox "here"

    Set sld = Application.ActiveWindow.View.Slide
    'If Wn.View.CurrentShowPosition = 5 Then
    If sld.SlideIndex = 5 Then


        For Each shp In sld.Shapes
            If shp.HasTextFrame Then
                MsgBox "looking"
                boxText = shp.TextFrame.TextRange.Text
                If InStr(1, boxText, "10 Seconds") <> 0 Then  'we found the countdown box
                    For i = 1 To 10
                        Pause (1)
                        If i < 9 Then
                            shp.TextFrame.TextRange.Text = 10 - i & " seconds"
                        Else
                            shp.TextFrame.TextRange.Text = 10 - i & " second"
                        End If
                    Next i
                End
            End
        Next shp

    ActivePresentation.SlideShowWindow.View.Next
    shp.TextFrame.TextRange.Text = "10 seconds"


   End If
End Sub

但我什至从未见过第一个 msgBox "here"...知道我哪里出错了吗?

我正在使用的文件位于 here。试图放入一些文本框和代码注释以明确我要做什么

你有一些编译错误。在 VB 编辑器中,select Debug > Compile VBAProject 你会看到:

Next shp: Next without For.

End 的两个实例更改为 End If


编辑:

  1. 根据提供的文件,存在 运行 次错误。 MsgBox "slideshow index is " & sld.SlideIndexSet sld = ... 之前 。调换两者的顺序。

  2. 此外,将Set sld = Application.ActiveWindow.View.Slide更改为Set sld = ActivePresentation.SlideShowWindow.View.Slide

  3. 请注意,InStr 搜索默认区分大小写。将 InStr(1, boxText, "10 Seconds") 更改为 InStr(1, boxText, "10 seconds"),或者只是 InStr(boxText, "10 seconds"),因为您使用的是小写字母 "seconds"。

  4. 您可能希望将 shp.TextFrame.TextRange.Text = "10 seconds" 移动到 Next i 之后,以确保重置 shp 文本。在测试中,演示文稿在最后一张幻灯片上的文本可以重置之前就结束了。可以调整代码以处理最后一张幻灯片的情况,并按照您原来的方法处理所有其他幻灯片。


完整代码:

Public Sub OnSlideShowPageChange(ByVal Wn As SlideShowWindow)

    Dim i As Integer
    Dim sld As Slide
    Dim shp As Shape
    Dim boxText As String

    Set sld = ActivePresentation.SlideShowWindow.View.Slide
    MsgBox "slideshow index is " & sld.SlideIndex

    If sld.SlideIndex = 5 Then
        For Each shp In sld.Shapes
            If shp.HasTextFrame Then
                boxText = shp.TextFrame.TextRange.Text
                If InStr(boxText, "10 seconds") <> 0 Then  'we found the countdown box
                    For i = 1 To 10
                        Pause (1)
                        If i < 9 Then
                            shp.TextFrame.TextRange.Text = 10 - i & " seconds"
                        Else
                            shp.TextFrame.TextRange.Text = 10 - i & " second"
                        End If
                    Next i

                    shp.TextFrame.TextRange.Text = "10 seconds"
                End If
            End If
        Next shp

        ActivePresentation.SlideShowWindow.View.Next
   End If
End Sub

这里是我得到的所有帮助后的最终解决方案...

Option Explicit

Public Function Pause(NumberOfSeconds As Variant)

'credit to 

    On Error GoTo Error_GoTo

    Dim PauseTime As Variant
    Dim Start As Variant
    Dim Elapsed As Variant

    PauseTime = NumberOfSeconds
    Start = Timer
    Elapsed = 0
    Do While Timer < Start + PauseTime
        Elapsed = Elapsed + 1
        If Timer = 0 Then
            ' Crossing midnight
            PauseTime = PauseTime - Elapsed
            Start = 0
            Elapsed = 0
        End If
        DoEvents
    Loop

Exit_GoTo:
    On Error GoTo 0
    Exit Function
Error_GoTo:
    Debug.Print Err.Number, Err.Description, Erl
    GoTo Exit_GoTo
End Function

Public Sub OnSlideShowPageChange(ByVal Wn As SlideShowWindow)

    Dim i As Integer
    Dim sld As Slide
    Dim shp As Shape
    Dim boxText As String
    Dim IsThisAQuestionSlide As Boolean

    IsThisAQuestionSlide = False

    Set sld = ActivePresentation.SlideShowWindow.View.Slide

    Select Case sld.SlideIndex
        Case 5: IsThisAQuestionSlide = True
        ' all the slide index #'s of question slides go here
    End Select


    If IsThisAQuestionSlide = True Then
        For Each shp In sld.Shapes
            If shp.HasTextFrame Then
                boxText = shp.TextFrame.TextRange.Text
                If InStr(boxText, "10 Seconds") <> 0 Then  'we found the countdown box
                    For i = 1 To 10
                        Pause (1)
                        If i < 9 Then
                            shp.TextFrame.TextRange.Text = 10 - i & " Seconds"
                        Else
                            shp.TextFrame.TextRange.Text = 10 - i & " Second"
                        End If
                    Next i
                    shp.TextFrame.TextRange.Text = "10 Seconds"
                End If
            End If
        Next shp

        ActivePresentation.SlideShowWindow.View.Next

   End If
End Sub