Quick Jump
Introduction | Example | Tutorial | Applications | Comments

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

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

Comments

Now it’s your turn! Leave a comment and let me know how I can help you. Remember - Automate Responsibly.

Discover how this Nuclear Engineer Mastered Excel VBA

And why you should, too


Your time is valuable. It's time to become a VBA expert.

About Ryan Wells


Ryan Wells

Ryan Wells is a Nuclear Engineer and professional VBA Developer. He is the lead developer of several VBA applications, including PDF VBA - a leading Excel Add-in for exporting Excel Objects, like charts and tables, to PDFs. Discover more of his popular Excel Add-ins, including Mouse To Macro and CF Shapes, at his dedicated Excel Add-ins page.