|
-
May 31st, 2010, 09:35 AM
#3
Thread Starter
Hyperactive Member
Re: ShellandWait problem
Thanks for quick reply.
The scheduled task is actually a Windows scheduled task; i.e., not specific to Excel. Any job can be started using DOS batch commands in a script.
The workbook is not normally started by any user. Windows starts it at a scheduled time daily.
The ShellandWait is a VBA routine. The DOS batch command script is passed as a parameter. Here's code:
Code:
Public Function ShellAndWait(ByVal batFile As String)
'
' Shells a new process and waits for it to complete.
' Calling application is totally non-responsive while
' new process executes.
'
Dim PID As Long
Dim hProcess As Long
Dim nRet As Long
'// Unlike other Functions Shell generates an error
'// instead of returning a 0 so handling the error
'// = Application NOT started.
On Error Resume Next
PID = Shell(batFile, vbMinimizedNoFocus)
If Err Then
'// handle the error here and End
'MsgBox "Could NOT execute:= " & batFile
errMsg = "Could NOT execute:= " & batFile
Exit Function
End If
On Error GoTo 0
'// SYNCHRONIZE For Windows NT/2000:
'// Enables using the process handle in any of the wait
'// functions to wait for the process to terminate.
'// obviously with NT you need access rights.
hProcess = OpenProcess(SYNCHRONIZE, False, PID)
'// Just set the dwMilliseconds to INFINITE to initiate a Loop
nRet = WaitForSingleObject(hProcess, INFINITE)
Do
GetExitCodeProcess hProcess, nRet
DoEvents
Loop While nRet = STILL_ACTIVE
CloseHandle hProcess
End Function
I use VBA. As I said, it could simply be something like initiating a wait loop, during which the user could hit ESC or some combination of keys that would exit the routine before the 'ShellandWait' is executed.
The suggestion to use a message box won't work because the program is normally started by Windows, middle of night, and there is no one to respond to the message.
Thanks.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|