Pearson Software Consulting Services
ShellAndWait - Waiting For A Shelled Process To Complete
This page has been replace by ShellAndWait.aspx | ||
Introduction To Shellnt> You can use the Shell command in VBA to start another program. Unlike programs started via Automation with the CreateObject function, you have no control over the Shell'd program. Your code can't issue commands to it, you can't stop it, nor can you directly determine when it ends. Shell is essentially the same as running a program from the Run dialog on the Windows Start menu. Shell And Wait Procedures When you use the Shell command in VBA to start another program, your VBA code continues executing immediately after starting the program with Shell. The VBA code does not wait for the program started by Shell to terminate. There are any number of sources on the internet for "Shell And Wait" type procedures, and they are all essentially the same -- they call the WaitForSingleObject Windows API function to wait for the process to close. The ShellAndWaitSimple procedure described on this page and available in this workbook is typical of all the other shell and wait procedures out there. It calls the Shell command, Opens the process, and then uses WaitForSingleObject to return when the shell'd program terminates. Nothing special. What is different on this page is the ShellAndWaitEnhanced procedure. This procedure allows you to display messages to the user on a control on a Userform. ShellAndWaitEnhanced will cycle through the messages you define, displaying each one for a prescribed interval. You can use these messages to inform the user that the Shell'd program is still running and that the system has not become "hung". I originally wrote ShellAndWaitEnhanced for a diagnostics and "detect and repair" type program for a large application. The diagnostics program used Shell to run the SysInfo program to generate a complete system information report file that would be sent to tech support staff. SysInfo can take several minutes to generate a complete report, and I wanted to periodically notify the user that the diagnostics program was still running as it should and that the neither the program nor the system had become hung. This page describes two procedures, ShellAndWaitSimple
and ShellAndWaitEnhanced, that you can use to cause your VBA code to wait
until the Shell'd program terminates. The code is too long to
cleanly present on a web page. You can
download the code
in an example workbook here. You can also download the
modShellAndWaitSimple and the
modShellAndWaitEnahanced modules separately.
These modules are entirely self-contained. You can import either or both of
the modules into your project and use the code exactly as it is written. No
modification or additional code is required. Each module contains the
constants, variables, functions, and Windows API declares required by its function. The ShellAndWaitSimple and ShellAndWaitEnhanced
functions may be used in any VBA code. There is nothing in the code that
restricts its usage to Excel. You can use the procedures in
Excel, Word, PowerPoint or any application that supports VBA. ShellAndWaitSimple
requires in VBA5 and ShellAndWaitEnhanced
requires in VBA6. If you attempt to open and run code in the
example workbook in Office 97, you'll get compiler errors in the
modShellAndWaitEnhanced, modTest, and UserForm1
modules. To work in Office 97, copy the modShellAndWaitSimple
module to another workbook. ShellAndWaitSimple ShellAndWaitSimple is a procedure that simply waits
for the Shell'd program to terminate. The function declaration is You provide in the parameter
A_7_AB_1_CommandLine
the command or name of the program that will be executed by the
Shell
function. No validation is performed to ensure that
A_7_AB_1_CommandLine
is a valid string for Shell. A error will occur and a message box
will be displayed if
A_7_AB_1_CommandLine is not valid. Dim Result As Boolean The
Result variable will receive the return value of
F_7_AB_1_ShellAndWaitSimple
. If an error occurs, Shell
may or may not have started the program, and the shell'd program may or may
not still be running. These conditions depend on the nature of the error. In
any case, if an error occurs, a message box is displayed indicating the
nature of the error. ShellAndWaitSimple will work in Office97 or later.
It will also work in Visual Basic version 6 with no changes. It has not been
tested in the NET framework. ShellAndWaitEnhanced The ShellAndWaitEnhanced is a procedure that waits for
the Shell'd program to terminate, in the same manner as ShellAndWaitSimple, but it will
periodically display messages on a Userform's control . These messages can indicate to the user
that the Shell'd program is still running and that the system has not become
hung. You can define whatever messages, and as many messages, as
you desire. The messages in the
A_7_AB_1_MessageArray array will be cycled at the
interval specified by
A_7_AB_1_MessageCycleTimeSeconds -- each
message is displayed for this many seconds. If you are
calling the ShellAndWaitEnhanced procedure from within a form, that form may
be displayed either modally or modelessly. If you call ShellAndWaitEnhanced
from a standard code module, and use a form only to display the messages,
that form must be displayed modelessly. The function declaration for ShellAndWaitEnhanced is
Public Function
F_7_AB_1_ShellAndWaitEnhanced( _ A_7_AB_1_CommandLine A_7_AB_1_MessageControl A_7_AB_1_MessageCycleTimeSeconds A_7_AB_1_MessageArray A_7_AB_1_DoEventsMilliseconds ShellAndWaitEnhanced requires Office 2000 or later. It has not been tested in the NET environment. A_7_AB_1_ErrorNumber A_7_AB_1_ErrorText A_7_AB_1_MessageOnClose
VB6 Versions Full documentation is provided in all the download files.
|
|
|
|
||
Created By Chip Pearson and
Pearson Software Consulting, LLC
This Page:
Updated: November 06, 2013
MAIN PAGE
About This Site
Consulting
Downloads
Page Index
Search
Topic Index
What's New
Links
Legalese And Disclaimers
chip@cpearson.com
© Copyright 1997-2007 Charles H. Pearson