| 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
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 |
||||||||||||||





"Launching" a Data File with Visual Basic |