Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shell from Excel to DOS - sequential processing - how? 1

Status
Not open for further replies.

rlingen

Technical User
Dec 6, 2000
41
CA
In several of my home built applications I shell out of Excel to run a DOS session (for example to run a PERL program or an FTP session). The default is for Excel to execute the shell, allow it to run, but then return immediately and continue processing VBA statements in parallel with the shell session.

I sometimes need Excel to wait until the DOS session is done its job before continuing, because I want VBA use the results of the shell session. I cannot find the appropriate commands in documentation, and have been accomplishing a pause by putting a dialog box on screen telling the user to wait for the session to end before pressing "ok", thus temporarily stopping VBA execution -- inelegant to say the least.

Am I missing something simple?

Roy
 
I have the same problem. Ever find a solution?
 
No, I am still using dialog boxes to stop execution. The user must wait until the DOS session is finished before clicking "OK".

Roy
 
Have you tried the ShellAndWait method? There is a FAQ in the VB 5&6 forum which illustrates the API call.
 

Not to totally side-track the issue, but if you're having fun with FTP, you can run that from Excel VBA so that Excel is always in control. Try searching for "wininet ftp vb" and you should find a few examples that will get you going.
 
When you run the Shell() function in a Visual Basic for Applications procedure, it starts an executable program asynchronously and returns control to the procedure. This shelled program continues to run independently of your procedure until you close it

--------------------
Option Explicit



Private Type STARTUPINFO
cb As Long
lpReserved As String
lpDesktop As String
lpTitle As String
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type

Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadID As Long
End Type

Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal _
hHandle As Long, ByVal dwMilliseconds As Long) As Long

Private Declare Function CreateProcessA Lib "kernel32" (ByVal _
lpApplicationName As Long, ByVal lpCommandLine As String, ByVal _
lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _
ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, _
lpStartupInfo As STARTUPINFO, lpProcessInformation As _
PROCESS_INFORMATION) As Long

Private Declare Function CloseHandle Lib "kernel32" (ByVal _
hObject As Long) As Long

Private Const NORMAL_PRIORITY_CLASS = &H20&
Private Const INFINITE = -1&


Public Sub ExecCmd(cmdline$)
Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO
Dim ReturnValue As Integer

' Initialize the STARTUPINFO structure:
start.cb = Len(start)

' Start the shelled application:
ReturnValue = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, _
NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)

' Wait for the shelled application to finish:
Do
ReturnValue = WaitForSingleObject(proc.hProcess, 0)
DoEvents
Loop Until ReturnValue <> 258

ReturnValue = CloseHandle(proc.hProcess)
End Sub

'To show the Sub
Sub Testing()
ExecCmd &quot;NOTEPAD.EXE&quot;
MsgBox &quot;Process Finished&quot;
End Sub

Best Regards

---
JoaoTL
NOSPAM_mail@jtl.co.pt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top