Andrej Kirejeŭ
Andrej Kirejeŭ

Reputation: 5481

How to pause a vbscript execution?

How can I pause an execution of a script from within? Something like Sleep WinAPI function?

Upvotes: 17

Views: 181928

Answers (3)

Sergey Enns
Sergey Enns

Reputation: 181

With 'Enter' is better use ReadLine() or Read(2), because key 'Enter' generate 2 symbols. If user enter any text next Pause() also wil be skipped even with Read(2). So ReadLine() is better:

Sub Pause()
    WScript.Echo ("Press Enter to continue")
    z = WScript.StdIn.ReadLine()
End Sub

More examples look in http://technet.microsoft.com/en-us/library/ee156589.aspx

Upvotes: 12

MacAnanny M
MacAnanny M

Reputation: 251

Script snip below creates a pause sub that displayes the pause text in a string and waits for the Enter key. z can be anything. Great if multilple user intervention required pauses are needed. I just keep it in my standard script template.

Pause("Press Enter to continue")

Sub Pause(strPause)
     WScript.Echo (strPause)
     z = WScript.StdIn.Read(1)
End Sub

Upvotes: 25

Oded
Oded

Reputation: 498972

You can use a WScript object and call the Sleep method on it:

Set WScript = CreateObject("WScript.Shell")
WScript.Sleep 2000 'Sleeps for 2 seconds

Another option is to import and use the WinAPI function directly (only works in VBA, thanks @Helen):

Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sleep 2000

Upvotes: 26

Related Questions