user2862496
user2862496

Reputation: 247

Time Delay in VBScript

i want to introduce a 3 hour delay between two consecutive lines in my VB script code.

I am using the following code snippet for this:

WScript.Sleep 10800000

But I think the code is stopping for a long time, much more than 3 hours. I used 10800000 as i read time is given in milliseconds.

Please let me know my mistake and the correct way to achieve this. Thank You!

Upvotes: 0

Views: 6051

Answers (2)

Kazimierz Jawor
Kazimierz Jawor

Reputation: 19067

Important This solution was provided because question was tagged at the beginning.

I will give you different option. I imagine you have something like this at the moment:

Sub MyCurrentSub()
    Dim A
    A = 10
    'wait 3 hour here and...
    '...after 3 hours do other part of the sub
    MsbBox A
End Sub

During waiting time, even if you use different option to wait (like Do...Loop) your Excel application will be limited, at least partially will not work as usually.

I would do it in this way, by creating and calling different Sub at the right moment:

Public A
Sub MyNewSub_1()
    A = 10
    Appication.OnTime Now + TimeValue("03:00:00"), "MyNewSub_2"
End Sub
'now you can use your Excel as usually...
Sub MyNewSub_2()
    MsgBox A
End Sub

You will get the same result, Excell will be free for you to use for 3 hours during waiting time. The only think you need to remember is to- DO NOT QUIT APPLICATION. If you Quit Excel it will 'forget' to call MyNewSub_2.

Upvotes: 1

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200233

Try something like this:

timeout = DateAdd("h", 3, Now)
Do Until Now > timeout
  WScript.Sleep 200
Loop

Upvotes: 2

Related Questions