AutoRun Macro with a delay to give user the choice to cancel the macro

W

wanderlust

Hi,
I am trying to write a macro that will execute when the workbook
is open. What I intend to do is to run this macro off of a batch file
that will open the file, run the macro and close the workbook.

Ideally, when the batch file kicks off the macro, I would like to
have a message pop up that says "Do you want to run the macro?" with
"yes" and "no" as the options. If there is no response within 10
seconds, go ahead and run the macro. This way, if some user opens the
file, but doesn't want to run the macro, the user can click on "no"
within 10 seconds and cancel the macro execution.

Thanks,
Ravi
 
A

Andrew Taylor

You can use the Popup method of the scripting shell object,
which has a timeout parameter.


Sub MessageWithTimeout()
Dim scriptshell As Object

Set scriptshell = CreateObject("wscript.shell")
Const TIMEOUT_SECONDS = 5

Select Case scriptshell.popup("What's the answer?",
TIMEOUT_SECONDS, "Select an answer", vbYesNo + vbQuestion)
Case vbYes
MsgBox "Yes"
Case vbNo
MsgBox "No"
Case -1
MsgBox "Timeout"
End Select

End Sub
 
W

wanderlust

You can use the Popup method of the scripting shell object,
which has a timeout parameter.

Sub MessageWithTimeout()
Dim scriptshell As Object

Set scriptshell = CreateObject("wscript.shell")
Const TIMEOUT_SECONDS = 5

Select Case scriptshell.popup("What's the answer?",
TIMEOUT_SECONDS, "Select an answer", vbYesNo + vbQuestion)
Case vbYes
MsgBox "Yes"
Case vbNo
MsgBox "No"
Case -1
MsgBox "Timeout"
End Select

End Sub

Worked like a Charm!!!!!!
Thanks for your help. I was trying to search on the groups for this
but, without success.
Ravi
 
Joined
Oct 20, 2011
Messages
1
Reaction score
0
I am trying to do something similar and came across this. Where did you put this script? in a batch file? or in excel's VBA?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top