472,129 Members | 1,680 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,129 software developers and data experts.

How to execute an external program and halting VBA code till external program finishes

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!

Thanks!
Greg Fierro
gf*****@SoftWerksDev.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 12 '05 #1
8 32443
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)
Nov 12 '05 #2
http://www.mvps.org/access/api/api0004.htm

Terry

"Trevor Best" <bouncer@localhost> wrote in message
news:gf********************************@4ax.com...
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)

Nov 12 '05 #3
On Thu, 4 Sep 2003 11:19:33 +0100 in comp.databases.ms-access, "Terry
Kreft" <te*********@mps.co.uk> wrote:
http://www.mvps.org/access/api/api0004.htm

Terry


Thanks Terry, I'm going to issue a memo to my programmers that any
code they cut and paste from the web should retain the original
comments including the author's name. You'll be glad to know I use
your "select folder" code a lot and that still has your name on it.
The reason I made that disclaimer in my followup was because I
suspected it came from *somewhere* and last time I posted code like
that I was accused of taking credit for someone else's work even
though anyone who knows my programming style could tell I didn't write
it.

--
A)bort, R)etry, I)nfluence with large hammer.

(replace sithlord with trevor for email)
Nov 12 '05 #4


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 12 '05 #5
Thanks Trevor and Terry for your answers to my problem. I have enought
to solve my problem!
Thanks Again!!
Greg Fierro

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 12 '05 #6
Trevor,
I wasn't being picky, just thought I'd answer the question about where it
came from.

Personally, I think most code that people write is bound to be influenced by
code they've seen, that's after all one of the best ways to learn, reading
and writing code.

I must admit though I always try to keep the original authors name in the
code, notably I have a number of routines from Stephen Lebans which I use,
they all keep the copyright and disclaimers through into my code ( that way
I can always blame him if it doesn't work <g>).

Terry
"Trevor Best" <bouncer@localhost> wrote in message
news:th********************************@4ax.com...
On Thu, 4 Sep 2003 11:19:33 +0100 in comp.databases.ms-access, "Terry
Kreft" <te*********@mps.co.uk> wrote:
http://www.mvps.org/access/api/api0004.htm

Terry


Thanks Terry, I'm going to issue a memo to my programmers that any
code they cut and paste from the web should retain the original
comments including the author's name. You'll be glad to know I use
your "select folder" code a lot and that still has your name on it.
The reason I made that disclaimer in my followup was because I
suspected it came from *somewhere* and last time I posted code like
that I was accused of taking credit for someone else's work even
though anyone who knows my programming style could tell I didn't write
it.

--
A)bort, R)etry, I)nfluence with large hammer.

(replace sithlord with trevor for email)

Nov 12 '05 #7
te*********@mps.co.uk (Terry Kreft) wrote in
<bj**********@newsreaderg1.core.theplanet.net>:
I must admit though I always try to keep the original authors name
in the code, notably I have a number of routines from Stephen
Lebans which I use, they all keep the copyright and disclaimers
through into my code ( that way I can always blame him if it
doesn't work <g>).


I also include the URL where I got it, where applicable.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 12 '05 #8
On Fri, 5 Sep 2003 10:32:53 +0100 in comp.databases.ms-access, "Terry
Kreft" <te*********@mps.co.uk> wrote:
Trevor,
I wasn't being picky


I didn't take it as such :-)

--
A)bort, R)etry, I)nfluence with large hammer.

(replace sithlord with trevor for email)
Nov 12 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Prashanth Badabagni | last post: by
3 posts views Thread by Bud Roble | last post: by
8 posts views Thread by saurabh.ss | last post: by
10 posts views Thread by Stefan Weber | last post: by
2 posts views Thread by andrews | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.