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.

I'll take a free VBA Developer Kit

Tutorial - WScript.Sleep

As always, remember text that looks like this can be modified. This style is typically reserved for file locations and file names.

  1. Create File
    1. Navigate to C:\VBScripts
    2. Create a new file named Timer.vbs
    3. Open Timer.vbs
      1. If necessary, right click and Open with Notepad
    4. Paste Exercise Break Reminder example in file
    5. Save File
  2. Execute VBScript
    There are a number of ways to execute a VBScript. I’ll present a few options.
    1. Run VBScript from Outlook Ribbon (My personal favorite)
    2. Double click the file
    3. Open Command Prompt cmd.exe
      1. Navigate to C:\VBScripts
      2. Type Timer.vbs
        or
      3. Type wscript Timer.vbs
        or
      4. Type cscript Timer.vbs

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.

VBScript Sleep Timer MsgBox

Application Ideas - WScript.Sleep

  1. Timer Ideas
    1. Exercise Timer
    2. Tea Timer
    3. Lunch Break Reminder
  2. Other Ideas
    1. Pause Code while External Application Loads
    2. Wait for a Connection to become Established
    3. Add a delay before using SendKeys
      1. Delay sending Login information
      2. Pause FTP commands
    4. Add a delay to a print job

When you’re ready to take your VBA to the next level, subscribe using the form below.