1

I am making my first Powerpoint 2007 macro and I am having a bit of trouble with it hanging, and not letting me move on to the next slide. I can press ESCAPE to quit the slideshow, but pressing space bar or anything else won't progress to the next slide. After a while, it just crashes. I come from a C++/Java background so I think its just something basic that I'm missing.

Basically I am trying to do a counter slide that counts the days/minutes/seconds from a particular date. When the slide loads I want it to show, in real time, how long its been since that date. I've put it through an infinite loop, which works fine to update the time, but then doesnt let me move on to the next slide.

Here's my code:

    Sub OnSlideShowPageChange(ByVal SSW As SlideShowWindow)
    'If SSW.View.CurrentShowPosition = 3 Then
    Do While SSW.View.CurrentShowPosition = 3    ' infinite loop
        Dim currentSlide As Integer
        currentSlide = SSW.View.CurrentShowPosition

        Dim startDate As Date
        Dim currentDate As Date
        Dim sngDiff As Single
        Dim lngDays As Long
        Dim lngHours As Long
        Dim lngMinutes As Long
        Dim lngSeconds As Long

        startDate = #7/22/2011 2:00:00 PM#
        currentDate = Now

        sngDiff = currentDate - startDate
        lngDays = CLng(sngDiff)
        sngDiff = sngDiff - lngDays
        lngHours = Hour(sngDiff)
        lngMinutes = Minute(sngDiff)
        lngSeconds = Second(sngDiff)

        With ActivePresentation.Slides(currentSlide)
            With .Shapes(2)
            .TextFrame.TextRange.Text = "It has been:" & lngDays & " Days " & lngHours & " hours " & lngMinutes & " minutes " & lngSeconds & " Seconds"
            End With
        End With

    DoEvents
    Loop
End Sub

Do I need to listen for some sort of button click to stop this infinite loop, or how do I do this?

Thanks.

3 Answers 3

0

A user form is something you add in the VBA editor; it's what you'd normally think of as a dialog box, though forms can be used for other things and needn't even become visible; that's what we're going to do here:

Option Explicit
Public bFormCodeRunning As Boolean

Sub FormDemo()

' Set a flag to let us know the code in the form
' is running
bFormCodeRunning = True

' "show" the form
UserForm1.Show vbModeless


End Sub

Sub KillForm()
' call this at some other point in the presentation
' when you're sure you're done running the form code

If Not bFormCodeRunning Then
    Unload UserForm1
End If

' You could actually call this from your slide change event handler

End Sub

Then Insert, User Form from the menu to add a new form; doubleclick it to view its code and add this:

Private Sub UserForm_Activate()

    ' Don't show my face
    Me.hide
    DoEvents

    ' prove that the form's loaded
    MsgBox "I'm well-formed"

    DoEvents
    ' and put your other code here

    ' and when the code's done, flag it
    bFormCodeRunning = False

End Sub
0

For doing a time delay in a VBA context it is usually better to use a form_timer object so in your code have:

If SSW.View.CurrentShowPosition = 3 Then
    Me.TimerInterval = 1000
Else
    Me.TimerInterval = 0
End If

Or something similar. Then in the form timer code have your clock update code

Private Sub Form_Timer()
// Your clock update code here
End Sub

It's been years since I've done any VBA so I'm a bit rusty but I hope this helps. In general use timers instead of loops for threading tasks, VBA doesn't cope well with them.

0

The problem is that your routine "owns" the app; until it exits, you won't be able to do anything manually (ie, advance to the next slide).

Whether or not you use a timer on a form (and fwiw, the Timer control isn't shipped with VBA as it is with VB), I think a form may be your solution.

Have your event handler load a form modelessly then exit.

The code in the form can then do any mods to slides or whatever else you want. Include DoEvents often enough that you don't slow down the main app, but the code in the form will run independently of what the main app is doing.

You don't need to make the form visible (and probably don't want to).

1
  • I've just started learning these Macros since yesterday. Would you be able to show me how to use these forms (small sample maybe)? I'm not familiar with VBA terminology yet. Is a form a type of Shape I put on the slide in question, or is it the name given to the marco itself?
    – eoinzy
    Jul 24, 2011 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.