On 04 Sep 2003 04:42:27 GMT in comp.databases.ms-access, Greg Fierro
<gf*****@softwerksdev.com> wrote:
I would appreciate any help from anyone with the following:
I have an external program (window32 based) that I am executing with the
VBA SHELL command. This program produces a text file which I have to
read after the external program finishes. I use the transfertext method
to read the file.
The problem is that the SHELL command executes external programs in an
asynch way. In other words, the VBA code will continue running and NOT
wait for the external program to finish.
I need to run this external program and HALT my VBA code until the
external program terminates. Is there any code out there or methods I
can use to do this??
Any help is greatly appreciated!
The code below will do it.
Disclaimer: I didn't write this code, I don't know where it came from
as the original author didn't comment it and credit him/herself and
I'm not taking credit for it.
--- code start ---
Option Compare Database
Option Explicit
Private Const STARTF_USESHOWWINDOW& = &H1
Private Const NORMAL_PRIORITY_CLASS = &H20
Private Const INFINITE = -1&
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
Public Sub ShellWait(Pathname As String, Optional WindowStyle As Long)
Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO
Dim RET As Long
' Initialize the STARTUPINFO structure:
With start
.cb = Len(start)
If Not IsMissing(WindowStyle) Then
.dwFlags = STARTF_USESHOWWINDOW
.wShowWindow = WindowStyle
End If
End With
' Start the shelled application:
RET& = CreateProcessA(0&, Pathname, 0&, 0&, 1&, _
NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
' Wait for the shelled application to finish:
RET& = WaitForSingleObject(proc.hProcess, INFINITE)
RET& = CloseHandle(proc.hProcess)
End Sub
--- code ends ---
--
A)bort, R)etry, I)nfluence with large hammer.
(replace sithlord with trevor for email)