General discussion

Locked

Excel VBA

By cha1608t.hkm ·
When I write VBA in excel, I have to use
shell command call for external program. How can I ensure that the statement after the shell statement will not run until the execution of external program completed.

This conversation is currently closed to new comments.

3 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

Excel VBA

by Peyison In reply to Excel VBA

The following routine should do what you're looking for. Pass the name of the external program, and it will wait until the program is done.

Public Sub ShellAndWait(asCommand As String, asCommandLine As String)
Dim lPid As Long
Dim lHnd As LongDim lRet As Long

lPid = Shell(asCommand & " " & asCommandLine)
If lPid <> 0 Then
lHnd = OpenProcess(SYNCHRONIZE, 0, lPid)
If lHnd <> 0 Then
lRet = WaitForSingleObject(lHnd, INFINITE)
CloseHandle (lHnd)
End If
End If



End Sub

Put these declares at the top of the module.

Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long

Private Const INFINITE = &HFFFF
Private Const SYNCHRONIZE =&H100000

Hope this helps.

Collapse -

Excel VBA

by cha1608t.hkm In reply to Excel VBA

Excellent.Thanks a lot!

Collapse -

Excel VBA

by cha1608t.hkm In reply to Excel VBA

This question was closed by the author

Back to Software Forum
3 total posts (Page 1 of 1)  

Related Discussions

Related Forums