Archive of
Previous Articles
Figure 1.
Pop-up menu for a text file
Figure 2.  Verbs of the txtfile file type
Since the verbs that you can supply to the ShellExecute
function are predefined in the registry, and since many
developers prefer not to muck with the registry, you can use the
simple
Verb Viewer utility to determine which verbs are
available for a given file type.

The
lpFile parameter is the path and name of the file that
you'd like the verb to be performed on. In fact, although the
most common use for
ShellExecute is to programmatically
manipulate data files and documents, the function can be used to
perform operations on any file system object, including drives
and folders.

lpParameters allows you to pass any necessary command
line parameters to the operation. If you don’t need to pass any
parameters, you can pass vbNullString as an argument.

lpDirectory is the working or current directory for the
operation.

Finally,
nShowCmd determines the state of the application
window if one is opened and can be one of the following
constants:

SW_HIDE
    Hides the application window

    SW_MAXIMIZE or SW_SHOWMAXIMIZED
    Activates and maximizes the application window.

    SW_MINIMIZE
    Minimizes the application window and activates the next
    top-level window in the z-order

    SW_RESTORE or SW_SHOW or SW_SHOWNORMAL
    Activates and displays the application window in its normal
    size and position.

    SW_SHOWMINIMIZED
    Activates and minimizes the application window

    SW_SHOWMINNOACTIVE
    Minimizes the application window and leaves the active
    window active.

    SW_SHOWNA and SW_SHOWNOACTIVATE
    Displays the window in its current state, and leaves the
    active window active.

    The function returns a value greater than 32 if it is
    successful. A return value from 0 to 32 indicates an error
    condition. This return value would seem to make
    ShellExecute less useful than the Shell function,
    which returns the task ID of the task it launches. This
    value can in turn be passed to the AppActivate statement
    to activate the newly launched application window. In
    addition, a variety of Win32 process control functions can
    be called once the OpenProcess function is called to
    retrieve the more commonly used process handle that
    corresponds to a given task or process ID. In contrast, with
    ShellExecute, you’re left to your own devices and
    your knowledge of cross-process control techniques in the
    Win32 API to effectively control the newly launched
    application and data file. The reality, though, is that in both
    cases, control of the newly launched application is difficult
    unless it exposes its object model using COM automation.
    This is especially true since both Shell and ShellExecute
    launch applications asynchronously.

    Note that, in most cases, it is the responsibility of the
    application to handle the information provided as
    arguments to the ShellExecute function and to behave
    accordingly, since the Windows shell simply passes
    them on to the application when it is invoked. As a
    result, it is not uncommon for some features
    (particularly working directories and the application
    window’s state) to not work properly.

    Once you understand the basic syntax of
    ShellExecute, and especially the meaning of its verb
    argument, launching an application that will open a
    data file is quite simple. The following code, for
    instance, prints a Word document to the default
    printer:
In this case, the Windows shell launches Microsoft Word, which
opens and prints the document and then closes automatically.
The following code opens an HTML page in a text editor, which
remains open for editing:
Dim sVerb As String
Dim sPath As String

sVerb = "Edit"
sPath = "C:\inetpub\wwwroot\index.html"

If Not ShellExecute(vbNull, sVerb, sPath, vbNullString, _
          vbNullString, SW_SHOWNORMAL) > 32 Then
MsgBox "Unable to " & sVerb & " " & sPath & "."
End If


Dim sVerb As String
Dim sPath As String

sVerb = "print"
sPath = "C:\Documents and Settings\All Users\Documents\Q3Report2003.doc"

If Not ShellExecute(vbNull, sVerb, sPath, vbNullString, _
          vbNullString, SW_HIDE) > 32 Then
MsgBox "Unable to " & sVerb & " " & sPath & "."
End If
Howling Wolf Consulting Services

"Launching" a Data File with Visual Basic
One of the limitations of Visual Basic is the absence of a language
construct that allows the developer to “launch” a data file or
document programmatically—that is, to provide the name of a
data file as an argument to a statement or function, and to have
that statement or function automatically launch the application
that can handle the data file, which in turn opens the document or
data file. Visual Basic includes the
Shell function to run an
external program, but the filename passed to it as an argument
must be an executable (an .exe or .com file), a batch file (.bat), or
a PIF file. Passing the name of a non-executable file to the
function generates runtime error 5, “Invalid procedure call or
argument.”

You can work around this limitation by examining the system
registry to determine what application is responsible for handling
a given file type. Once you’ve determined the application capable
of handling the given file type, you can use the
Shell function
to launch it and open the data file. This, however, is rather
cumbersome. Instead, a call to a single Win32 API function,
either
ShellExecute or ShellExecuteEx, can be used to
perform an operation on a particular file or document. In this
article, we’ll examine
ShellExecute. Its syntax is:









The first parameter,
hwnd, defines the window that owns any
dialogs displayed as a result of the
ShellExecute call. You
can either pass the window handle of one of your application’s
forms (such as
Me.hwnd) or, if you don’t care about dialogs,
you can simply pass the constant
vbNull.

The second parameter,
lpOperation, is really the critical
component of the
ShellExecute function. Because it’s also
one that many developers find confusing, it’s worth spending
some time discussing it.
lpOperation is a string whose value
is a verb identifying the operation to perform. Although “open”
is  both the most common and the default verb, the actual verbs
associated with a given file type are arbitrary and must be defined
in the system registry. A verb is simply a subkey of
HKEY_CLASSES_ROOT\<file identifier>\Shell.
The verb’s
Command subkey in turn provides a command to be
carried out whenever the verb is invoked.

To see how this works, let’s use a text file (a file with an
extension of .txt) as an example. When we right-click on a text
file in an Explorer window, a pop-up menu like the one shown in
Figure 1 appears. The “Open”, “Print”, and “Edit” entries in the
upper portion of the pop-up menu are defined by verbs found in
the system registry. To produce this portion of the context menu,
the Windows shell does the following:

  1. Extracts the extension of the file and looks for the file
    extension key, in this case the key named
    HKEY_CLASSES_ROOT\.txt.
  2. Retrieves the file extension key’s default value, in this case
    txtfile. It uses this to open the file identification key, in
    this case HKEY_CLASSES_ROOT\txtfile.
  3. Retrieves the verbs associated with the txtfile file type
    by enumerating the subkeys of
    HKEY_CLASSES_ROOT\txtfile\shell.
  4. Checks whether the verb’s key has a default value. If so,
    that string is displayed. If not, the verb is displayed, unless
    it is one of five canonical verbs (open, print, explore, find,
    openas), which are localized and displayed in proper case.

If the user double-clicks on a text file, the shell performs similar
steps, except that rather than iterating the verb keys, it simply
retrieves the default value of the

HKEY_CLASSES_ROOT\txtfile\shell
key, which
defines the default verb. If it is present, it simply executes the
command associated with that verb. If not, it executes the
command associated with  
HKEY_CLASSES_ROOT\txtfile\shell\open.

If we use RegEdit to examine the
HKEY_CLASSES_ROOT\txtfile\shell key in the
registry, we should see something that resembles Figure 2. Note
that the Open and Print menu items correspond to the
open and
print verb keys in the registry. The two additional menu items,
Edit and Open With…, are provided for every file system item.
An additional verb defined in the registry,
printto, does not
appear on the menu, since it is used by the system solely for
handling drag-and-drop printer operations.
Public Declare Function ShellExecute Lib "shell32.dll" _
              Alias "ShellExecuteA" _
(ByVal hwnd As Long, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long