473,232 Members | 1,596 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,232 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 32623
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
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.