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

时间:2019-03-03 20:14:38

标签: vba powerpoint

我正在使用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。试图放入一些文本框和代码注释,以明确我要做什么

2 个答案:

答案 0 :(得分:1)

您遇到了一些编译错误。在VB编辑器中,选择 Debug > Compile VBAProject ,您将看到:

  

Next shp:   下一个没有For。

End的两个实例更改为End If


编辑

  1. 基于提供的文件,存在运行时错误。 MsgBox "slideshow index is " & sld.SlideIndex早于{em> Set 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"),因为您使用的是小写的“秒”。

  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

答案 1 :(得分:0)

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

Option Explicit

Public Function Pause(NumberOfSeconds As Variant)

'credit to https://stackoverflow.com/questions/6960434/timing-delays-in-vba#_=_

    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