Sleep/Wait

3 posts / 0 new
Last post
Ernesto Gurgel ...
Offline
Joined: 07/18/2020 - 07:15
Sleep/Wait

Hi, 
I'm trying to make a script wait a certain amount of time before continuing. The reason is to wait for it to completely send the email with the excel files and then delete the folder containing the files.
 
 Set WScript = CreateObject("WScript.Shell")  Dim StartTime, EndTime As Double StartTime = Format$(Now() , "hh:mm:ss")     MsgBox "StartTime: "& StartTime
 WScript.Sleep 1000
 EndTime = Format$(Now() , "hh:mm:ss")     MsgBox "EndTime: "& EndTime WScript = Nothing

Brian Element's picture
Brian Element
Offline
Joined: 07/11/2012 - 19:57

Hi Ernesto, this is an interesting question and I came up with a different solution.  There is a function called Timer that "Tracks elapsed time" per the Language Browser.  I used this in a loop to cause the delay.  I first get the current time, I then create a second variable and add 5 seconds to it (or how ever many seconds you want).  I think place it in a loop and keep checking the current time, once the current time is greater than the second variable then the loop ends.  The problem with this is it won't work around midnight without adding move code to it but if you are running the script during the day you shouldn't have that problem.

 


Sub Main
	Dim tmCurrentTime As Long
	Dim tmFinishTime As Long
	    
	'Get the current time
	tmCurrentTime = Timer
	
	'Add 5 seconds to the current time
	tmFinishTime = tmCurrentTime + 5
	 
	'Keep looping until the current time is greater than the finish time
	'Note this probably won't work if the current time is before midnight
	'and the finish time is after midnight.   
	Do While tmCurrentTime < tmFinishTime
		tmCurrentTime = Timer
	Loop
	    
	MsgBox "It worked"
End Sub
Ernesto Gurgel ...
Offline
Joined: 07/18/2020 - 07:15

Thank you, Brian! You are really great.