473,396 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 32638
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Prashanth Badabagni | last post by:
Hi , I'm Prashanth Badabagni .. I have and idea how a program prints it's own source code .. void main() { FILE *P; char *file,c; strcpy(file,__FILE__); p=fopen(file,"r");
16
by: Puneet | last post by:
Hi ALL, I have a silly question... (may be) Can we write a single line C program whose output is the program itself? Is anybody know the answer please tell me. Puneet
2
by: nick | last post by:
Hi, I wondering if it is possible in asp.net to somehow execute a string of code as if it were a page on the server. That is to say, if I executed the following code which resided in an aspx...
3
by: Bud Roble | last post by:
Does anyone have some sample code for a VB.NET Service program. I need the service program to sleep and come alive every night around midnight. The program needs to open an MDB database, update...
8
by: saurabh.ss | last post by:
Hi I want to measure the time required to execute a piece of code. For example, I want to know the time required to execute a given function. How do I do it within C++? I know I got to use a...
2
by: techsatish | last post by:
Hi, I want the double click event to call(here not able to get the MouseEventArgs) and execute the mouseup event code or any one tell me how to make my treecontrol double click nodes to behave...
1
by: pixcee2000 | last post by:
hi plz tell code in java program form?
10
by: Stefan Weber | last post by:
Hi, I'm trying to access the JavaScript code contained in a <scripttag via its "text" attribute. This works well, if the code is embedded in the HTML page. However, when the code is in an...
2
by: andrews | last post by:
With System.Diagnostics.Process.Start(ProgramPath) I can start a program X in a vb.net program Can I also dedect and how in this vb.net program that X is closed? Thanks for any response
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.