1

I'm using PowerPoint 2016.

I have found other questions on this forum (like here) that indicate the answer is to use the OnSlideShowPageChange or slideshownextslide events. However, it seems to me that these events do not fire.

I have the following code in a module in my presentation

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

But I never even see that first msgBox "here"....any idea where I'm going wrong?

The file I'm using is located here. Tried to put in some text boxes and code comments to make it clear what I'm looking to do

jerH
  • 1,085
  • 1
  • 12
  • 30

2 Answers2

1

You've got some compile errors. In the VB editor, select Debug > Compile VBAProject and you'll see that:

Next shp: Next without For.

Change the two instances of End to End If.


EDIT:

  1. Based on the file provided, there's a run-time error. MsgBox "slideshow index is " & sld.SlideIndex comes before Set sld = .... Switch the order of the two.

  2. Additionally, change Set sld = Application.ActiveWindow.View.Slide to Set sld = ActivePresentation.SlideShowWindow.View.Slide

  3. Note that InStr search is case-sensitive by default. Change InStr(1, boxText, "10 Seconds") to InStr(1, boxText, "10 seconds"), or just InStr(boxText, "10 seconds"), since you are using lowercase "seconds".

  4. You might want to move the shp.TextFrame.TextRange.Text = "10 seconds" to after Next i to ensure that the shp text is reset. In testing, the presentation ended before the text could be reset on the last slide. The code could be tweaked to handle the case of the last slide and follow your original approach for all other slides.


Full code:

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
BigBen
  • 46,229
  • 7
  • 24
  • 40
  • Duh. Yes, I do have a pause function...this still isn't working right, but at least I'm getting into the event handler. Thanks! I'm far more familiar with VBA in Excel than in PowerPoint.... – jerH Mar 03 '19 at 20:46
  • I guessed as much on the pause function - I'll edit that out. Me too, much more familiar with Excel than PowerPoint. – BigBen Mar 03 '19 at 20:47
  • Retract my "answered" - love microsoft...this "worked" once, in the sense that I got into the event handler and saw the "here" message. Hasn't worked since. I can compile with no errors, but I never seem to enter the event handler – jerH Mar 03 '19 at 23:05
  • Added a link to the file I'm using in the OP – jerH Mar 03 '19 at 23:17
  • OnSlideShowPageChange doesn't always fire for some reason. The fix is to add an ActiveX control to one of the slides in your presentation; it can sit off the slide if you like. If for some reason it breaks, use a fix that works for some other reason. Life in PowerPointLand. ;-) – Steve Rindsberg Mar 05 '19 at 15:57
  • @SteveRindsberg well that's weird... thanks for the detail. – BigBen Mar 05 '19 at 16:26
  • @BigBen thanks...I'll try to get to that this evening. Looks like I made some pretty stupid mistakes :) – jerH Mar 06 '19 at 03:04
  • @SteveRindsberg I had run across posts that said that, but I do have a command button on a slide and it doesn't make a difference. One thing that is unclear to me though (again, I'm way more familiar with Excel VBA than PowerPoint) is that in the Excel developer tab under insert there are distinct "form controls" and "activeX controls"...in PowerPoint there are only "controls". Are they ActiveX? – jerH Mar 06 '19 at 03:06
  • Nothing that a bit of debugging won't solve. I got it to work on my end - see it [here](https://drive.google.com/file/d/113rEp1zYEee3isJ2mAsbfkKCeKgyu_C6/view?usp=sharing). – BigBen Mar 06 '19 at 03:13
  • @jerH Yes, it's the gadgets in the Controls group of the Developer tab you're after. If BigBen's got it working, please do post the results back here so it's all neatly packaged up in one thread. Thanks! – Steve Rindsberg Mar 06 '19 at 15:51
0

Here was the final solution after all the help I got here...

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
jerH
  • 1,085
  • 1
  • 12
  • 30