Previous Page
Next Page

While...Wend

One more kind of looping technology is the While...Wend statement. It is read as follows: "While statement A is true, we will continue to loop through the code. Once it is met, then we will exit at the Wend statement." It is very similar to a Do...Until loop statement. The following script, WhileWendLoop.vbs, illustrates using this construction.

The WhileWendLoop.vbs script is a timer script. We create a time stamp by using the timeserial function. If you look up timeserial in the My Documents\Microsoft Press\VBScriptSBS\Resources\Scripts56.chm file, it will tell you that it takes three numbers (hour, minute, second) and turns them into a date variantwhich means it will turn them into a time stamp we can use.

In the subBeep subroutine, we use the Run method to create a beep. Subroutines are discussed in chapter 15 (Using Subs and Functions). For now, you can think of a subroutine as a special part of the script we can access by name. In this script, we use the subroutine to keep the details of creating a beep from the main script. Later, we may want to change the beep to something else ... which could be done by replacing the subroutine with some other code. If it was embedded in the worker section of the script, we would have to make many more changes. We do this once the time has been reached that was specified in the dtmTime variable.

To use the WhileWendLoop.vbs script, you will need to pick a time (in 24-hour time format) that is about a minute in the future; make sure you supply that time to the dteTime variable in the Reference section of the script. Then run the script. Once the time is reached, the script will beep and print a message indicating that the time has been reached.

WhileWendLoop.vbs

Option Explicit
'On Error Resume Next
dim dtmTime

Const hideWindow = 0
Const sleepyTime = 1000
dtmTime = timeSerial(19,25,00) <;$QS>Modify this value with desired time

while dtmTime > Time
WScript.Echo "current time is: " & Time &_
" counting to " & dtmTime
WScript.Sleep sleepyTime
Wend
subBeep
WScript.Echo dtmTime & " was reached."

Sub subBeep
Dim objShell
Set objShell = CreateObject("WScript.Shell")
objShell.Run "%comspec% /c echo " & Chr(07),hideWindow
End Sub


Previous Page
Next Page