[ Team LiB ] |
Recipe 11.7 Run the Application Associated with a Data File11.7.1 ProblemYou'd like to find a way to provide a list of existing files, allow users to select a file, and run the appropriate application for that file. Windows knows how to do this—for instance, when you double-click on a file with a .TXT extension in Explorer, Windows runs Notepad with that file. How can you provide this sort of functionality in your own applications? 11.7.2 SolutionWindows provides two API functions, FindExecutable and ShellExecute, that make running a related application possible from within Access. Both functions rely heavily on the Windows registry, which tracks the relationships between filename extensions and related executable programs. Figure 11-8 shows the results of running the REGEDIT.EXE program, which ships as part of Windows. REGEDIT allows you to add, edit, modify, or delete file associations. (The registry editor is named REGEDT32.EXE under Windows NT and, though it looks different, it functions in a similar manner.) Figure 11-8. REGEDIT.EXE, showing file types registered on a typical system
In this solution, you use the FindExecutable function to get the name of the executable file associated with a selected data file. You also use the ShellExecute function to run the executable file, with the selected data file opened and ready to edit. Load and run frmTestExecute, shown in Figure 11-9. To use this form, select a path (it defaults to your Windows directory when it first loads). Once the list box fills with all the files in the specified directory, click on one with the mouse. If there's an active file association for the selected file, the form will display that executable filename in a text box. If there's an associated executable file, you can run it and load your chosen file by double-clicking on the list box or clicking on the checkmark button. Figure 11-9. The sample form, frmTestExecute, from 11-07.MDBTo use this functionality in your own applications, follow these steps:
The FindExecutable function returns an integer error code. If the value is greater than 32, the function has succeeded. Otherwise, it returns one of the error codes in Table 11-6 (note that these error codes are shared by several functions). If the function succeeded, strResult will be a null-terminated string containing the associated executable file. You'll need to trim off that trailing null character. One easy way to do this is by using the TrimNull function in basShellAPI, as follows: Private Function TrimNull(strValue As String) ' Trim strValue at the first ' null character you find. Dim intPos As Integer intPos = InStr(strValue, vbNullChar) If intPos > 0 Then TrimNull = Left$(strValue, intPos - 1) Else TrimNull = strValue End If End Function
For example, the following code will find the executable file associated with MyFile.OOG: Dim strBuffer As String Dim strResult As String strBuffer = Space(128) strResult = "" intRetval = acb_apiFindExecutable("MyFile.OOG", ".", strBuffer) If intRetval > acbcHinstanceErr Then ' Use the TrimNull function in basShellAPI ' to remove the trailing null character. strResult = TrimNull(strBuffer) End If ' Now, strResult holds either "" or the name ' of the executable you need. To make this simpler, basShellAPI includes the acbFindExecutable function. This function requires the same parameters and returns the same values as acb_apiFindExecutable, but it handles the details of initializing the string buffer and trimming off the trailing null character for you. You'll want to use this function instead of calling the Windows API directly, as it will ensure that you use the correct methods for sending and receiving strings. Once you know the name of the executable file associated with the selected document, you'll want to execute it with the ShellExecute API function. You could, of course, use the Shell command, but ShellExecute gives you a bit more flexibility, as a comparison of the two shows:
To use the ShellExecute function, call it with the six parameters shown in Table 11-7.
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.
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) 11.7.3 DiscussionYou 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 rely heavily on the Windows registry. It may be useful to dig through the file associations in the registry (as discussed in the earlier sidebar) and see how Windows finds applications itself when you double-click on data files.
|
[ Team LiB ] |