Online Book Reader

Home Category

Access Cookbook - Ken Getz [235]

By Root 2070 0
code to trap and deal with errors. In the long run, using ShellExecute is simpler.

ShellExecute allows you to specify the default drive/directory for your application. Shell does not.

ShellExecute provides a few more options than Shell; see Table 11-8 for details.

Not that you'll use it often, but ShellExecute allows you to specify the action to take on opening a file. If you want to print the file rather than open it, specify the "print" operation for the second parameter.

TIP

If your only intent is to run the executable associated with a file, you don't need to call FindExecutable explicitly. Instead, you can pass the file name to ShellExecute, and it will find the executable for you. In this example, we wanted to display the associated executable, so we divided the task into two API function calls.

To use the ShellExecute function, call it with the six parameters shown in Table 11-7.

Table 11-7. Parameters for the ShellExecute API function

Parameter

Type

Description

hWnd

Integer

The handle of the window to be used as the parent for message boxes that may appear.

strOp

String

The operation to perform. Normally, can only be "open" or "print".

strFile

String

The name of the program to start.

strParams

String

Command-line arguments for the executable program. Normally, the name of the file to load into the application.

strDir

String

The default drive/directory for the application when it starts up.

intShowCmd

Integer

Specification of how to show the new window when the application starts up. For a list of values, see Table 11-8.

Table 11-8 lists all the possible values for the intShowCmd parameter. These values control how the new application's window appears on the Windows desktop.

Table 11-8. Window display options for the intShowCmd parameter to ShellExecute

Constant

Value

Meaning

acbSW_HIDE

0

The window is hidden when started.

acbSW_SHOWNORMAL

1

The window is restored to its previous state (neither minimized nor maximized).

acbSW_SHOWMINIMIZED

2

The window is made visible and minimized.

acbSW_SHOWMAXIMIZED

3

The window is made visible and maximized.

acbSW_SHOWNOACTIVATE

4

The window is displayed, but doesn't gain the input focus.

acbSW_MINIMIZE

6

The window is minimized (as an icon) when started.

acbSW_SHOWMINNOACTIVE

7

The window is made visible and minimized, but doesn't receive the input focus.

acbSW_SHOWNA

8

The window is displayed without any change to the window's state (remains minimized, normal, or maximized).

acbSW_RESTORE

9

The window is restored to its previous state (neither minimized nor maximized). (Same as acbSW_SHOWNORMAL.)

For example, to run the program C:\OOGLY\MKOOGLE.EXE (which created MyFile.OOG) maximized on the screen, you could run code like this from a form's module:

intRetval = acb_apiShellExecute(Me.hWnd, "open", "C:\OOGLY\MKOOGLE.EXE", _

"MyFile.OOG", "C:\OOGLY", acbSW_SHOWMAXIMIZED)

Discussion


You can call the FindExecutable function to retrieve an associated executable file for a given document, and then pass both the executable name and the document name to ShellExecute to load them. For example, you might use code like this in your application:

Dim intRetval As Integer

Dim strBuffer As String

intRetval = acbFindExecutable("MyFile.XXX", ".", strBuffer)

If intRetval <= acbHInstanceErr Then

MsgBox "Unable to find executable. Error " & intRetval & "."

Else

' You're only here if you found the executable.

intRetval = acb_apiShellExecute(Me.hWnd, "open", strBuffer, _

"MyFile.XXX", "C:\NewDocs", acbSW_SHOWMAXIMIZED)

If intRetval <= acbHInstanceErr Then

MsgBox "Unable to load application. Error " & intRetval & "."

End If

End If

You may find it interesting to work your way through the sample form frmTestExecute. It uses the AddItem method of the ListBox control (added in Access 2002) to add file names retrieved from a Collection object. The code fills the collection by calling the FillDirlist method, in the basFillList module.

The methods presented in this solution

Return Main Page Previous Page Next Page

®Online Book Reader