Introduction | Example | Tutorial | Applications
Introduction - WScript.Sleep
Creating a VBScript countdown timer is easy with the WScript.Sleep method. WScript.Sleep reads in one variable - integer time in milliseconds. WScript.Sleep will suspend your script for the indicated number of milliseconds.
Suspension is useful when you need to add a delay or a pause to your project. For example, a delay is often necessary when sending keystrokes via VBScript (more to come on that in a later post). Another neat use for the WScript.Sleep
function is to create a timer or reminder.
Example - WScript.Sleep
Desktop Exercise Reminder
dTimer=InputBox("Enter timer interval in minutes","Set Timer") 'minutes
do until IsNumeric(dTimer)=True
dTimer=InputBox("Invalid Entry" & vbnewline & vbnewline & _
"Enter timer interval in minutes","Set Timer") 'minutes
loop
if dTimer<>"" then
do
WScript.Sleep dTimer*60*1000 'convert from minutes to milliseconds
t=MsgBox("Take a Walk." & vbnewline & vbnewline & "Restart Timer?", _
vbYesNo, "It's been " & dTimer &" minute(s)")
if t=6 then 'if yes
'continue loop
else 'exit loop
exit do
end if
loop
end if
Make powerful macros with our free VBA Developer Kit It’s easy to copy and paste a macro like this, but it’s harder make one on your own. To help you make macros like this, we built a free VBA Developer Kit and wrote the Big Book of Excel VBA Macros full of hundreds of pre-built macros to help you master file I/O, arrays, strings and more - grab your free copy below.
Tutorial - WScript.Sleep
As always, remember text that looks like
- Create File
- Navigate to
C:\VBScripts - Create a new file named
Timer.vbs - Open
Timer.vbs - If necessary, right click and Open with Notepad
- Paste Exercise Break Reminder example in file
- Save File
- Navigate to
- Execute VBScript
There are a number of ways to execute a VBScript. I’ll present a few options.- Run VBScript from Outlook Ribbon (My personal favorite)
- Double click the file
- Open Command Prompt
cmd.exe
- Navigate to
C:\VBScripts - Type
Timer.vbs
or - Type
wscript Timer.vbs
or - Type
cscript Timer.vbs
- Navigate to
When executed, this VBScript example prompts the user to set the timer using the InputBox
function. Each time the timer expires, a MsgBox
gives you the option to restart the timer. I use this script to remind me to stretch my legs every 45 minutes at the office.
Application Ideas - WScript.Sleep
- Timer Ideas
- Exercise Timer
- Tea Timer
- Lunch Break Reminder
- Other Ideas
- Pause Code while External Application Loads
- Wait for a Connection to become Established
- Add a delay before using SendKeys
- Delay sending Login information
- Pause FTP commands
- Add a delay to a print job
When you’re ready to take your VBA to the next level, subscribe using the form below.