• No results found

Most actions in PowerPoint happen be cause the user did something, such as pressing a but ton to go to another slide. Sometimes, how ever, you want things to hap pen whether or not the user has done anything. For ex ample, you might want a sound to start playing a few seconds after the slide is shown. You might want the presentation to go from slide to slide on its own. You might want in for- mation to pop up on the screen, then go away, and then have other in formation pop up on the screen.

As soon as the user clicks on a but ton tied to a script with timing features (such as the but ton to go to another slide), you can start any thing hap pening af ter Timed Functions 125

any length of time. Of course, the standard “Custom Animation” choice from the Slide Show menu can al low ob jects to appear with timing, but you might want to do more. If you want something to happen after a short delay, you can use the following procedure:

Sub Wait() waitTime = 5 start = Timer

While Timer < start + waitTime DoEvents

Wend End Sub

This procedure waits five seconds.Timer is a func tion that re turns the num ber of seconds since midnight (e.g., at 12:01 A.M.,Timer will re turn 60). waitTime

is a vari able used to tell how many seconds to wait (change the number 5 to any number to have this pro cedure wait that number of seconds). At the beginning of the pro cedure, the variablestart is set to the current time in sec onds (as re - turned by Timer). Next, we loop un til the cur rent time is less than the time we started plus the waitTime (which is five seconds in our example). In side the loop (be tween theWhile state ment and theWend state ment), we runDoEvents. This lets VBA check to see if anything else is hap pening, particularly things that the user might do, such as hit the Es cape key or click on another button. If you don’t want the user to do anything while you are waiting, leave outDoEvents.

Be careful! If you make a mistake (perhaps you set thewaitTime to five mil - lion seconds in stead of five seconds or you mistypedTimer in the Do While state - ment), you could end up in an in finite loop, essentially freezing PowerPoint. If you feel you must stop the user from do ing any thing while VBA waits, leaveDoEvents

in your pro cedure un til you are sure everything works. Once you are certain every- thing works, delete the DoEvents line. This will allow you to stop your presenta- tion by hit ting the Es cape key while you are still testing your pro cedure.

Before we con tinue, get a new PowerPoint pre sentation and type theWait

pro ce dure. Then add the fol low ing pro ce dure:

Sub HelloWaitGoodbye() MsgBox ("Hello") Wait

MsgBox ("Good bye") End Sub

When you run HelloWaitGoodbye, you should see a MsgBox that says “Hello.” Af ter you click OK to dis miss the MsgBox, you should see aMsgBox

that says “Goodbye,” but only after a de lay of five seconds.

Now suppose that you want to wait, but not always for five seconds. You could write sev eral dif fer ent pro ce dures (Wait5,Wait10,Wait60, etc.) to wait different amounts of time, but we can use a sim ple parameter to write one proce- dure that can wait dif ferent amounts of time.

Sub Wait(waitTime As Long) start = Timer

While Timer < start + waitTime DoEvents

Wend End Sub

In this pro cedure, instead of setting thewaitTime to five, we callWait with however long we want to wait (e.g.,Wait (60) would wait sixty seconds).

Timed func tions are useful if you want to give your users a chance to do something be fore moving on. For example, you might display a text box, wait a short time, then display a second text box. This allows the user to focus on the first text box be fore getting too much in formation. Be careful with timed func- tions, be cause dif ferent people read at dif ferent speeds. If you set your wait times too long, some people will get restless waiting for the next thing to hap pen. If you set them too short, some peo ple will not have time to finish the first thing.

Some tim ing can be done automatically without VBA. You can use Custom Animation to have things ap pear and disappear as much as you like. How ever, as with many things that you can do without VBA, you might find that you can do more with VBA. For ex ample, you might ask the user how fast to go:

speed = InputBox ("How fast do you read [fast, me dium, slow]?")

Now when it is time to wait, you might do something like the following:

If speed = "fast" Then Wait (5)

ElseIf speed = "me dium" Then Wait (10)

Else

Wait (15) End If

You should note that wait times are approximate. This does not work well if you need precise timing, but it should do roughly what you want.