Page 472 - Microsoft Office Excel 2003 Programming Inside Out
P. 472
Microsoft Office Excel 2003 Programming Inside Out
There are a variety of reasons why you might need to interact with an additional application.
For example, you might want to use the Calculator, Character Map, or even a DOS batch file
from Excel. In fact, you can execute a Control Panel application, if necessary. To accomplish
these tasks using VBA code, use the Shell function. The following procedure starts the Calcu
lator application using the Shell function:
Sub RunCalculator()
On Error Resume Next
Program = "calc.exe"
TaskID = Shell(Program, vbNormalFocus)
If Err <> 0 Then
MsgBox "Unable to start " & Program, vbCritical, "Error"
End If
End Sub
The Shell function returns the task identification number for the application. The task iden
tification number can be used in later code to activate the task. The second argument for the
Shell function determines the window state of the application, such as minimized, maximized,
hidden, or normal. If the function generates an error, the On Error statement will display a
message indicating that the file can’t be found.
Note Remember that if you have subsequent code following the Shell function, the code
Chapter 21
will be executed immediately. If an instruction requires user intervention, Excel’s title bar
flashes to notify the user while the other application is active.
An alternate method used to start an application is using the Start command. The Start com
mand can be executed from the Run dialog box or directly from a DOS window. This command
is available in most versions of Microsoft Windows. The Start command is used to start a
Windows-based application from a DOS window. Using the Start command doesn’t require
the code to open the program associated with the document; the command uses the program
associated with the filename to open the application as well as the file. You are required to
enter the full path to the file and extension for this command to execute properly. Figure 21-1
shows an example of how to open the Fall Initiative.ppt file using the Start command.
Figure 21-1. You can use a Start command in the Run dialog box to open a presentation file.
446
Part 6: Excel and the Outside World: Collaborating Made Easy

