Home > Back-end >  VBA in PowerPoint only working with a stop and F8 stepping through it, When running by itself it cha
VBA in PowerPoint only working with a stop and F8 stepping through it, When running by itself it cha

Time:11-24

This code is used to do a timing sequence and depends on each segment starting at the correct interval which is set by a variable based on user input. With stops put in and stepping through the code it works correctly with no errors. When executed without a stop the command button "RunCommand" stays depressed for 5 seconds and shape 1's forecolor changes but shape 2 does not change. I have drummed the code down to this for ease as this seams to be the part given me the issue. I'm stuck, if I just run the code it does not work, if I step through the code it works fine?????

Private Sub RunCommand_Click()

Dim EndTick As Date

EndTick = DateAdd("s", 5, Now())
    
Set myDocument = ActivePresentation.Slides(1)

    With myDocument.Shapes("Arrow3rd2").Fill
        .ForeColor.RGB = RGB(255, 0, 0)
    End With
    
    Do
        If Now() > EndTick Then
            With myDocument.Shapes("Arrow3rd1").Fill
                .ForeColor.RGB = RGB(255, 0, 0)
            End With
        Else
            DoEvents
        End If
    Loop Until Now() >= EndTick
    
End Sub

CodePudding user response:

The value for Application.now will change most likely during the DoEvents. So your program flow is

Check if now > EndTick: If yes, change color of shape.
If this is not the case, give time to do something else (DoEvents)
--> During this, now will increase.
Check again if now > EndTick: If yes, leave the loop.

So the part where the color is changed is probably not hit.

Try to change the logic so that the If is only hit once (code is untested)

Do
    DoEvents
Loop Until Now() >= EndTick
With myDocument.Shapes("Arrow3rd1").Fill
    .ForeColor.RGB = RGB(255, 0, 0)
End With
  • Related