ACC: Start Files or Hyperlinks with Windows API ShellExecute()

ID: Q148632

The information in this article applies to:

SUMMARY

Advanced: Requires expert coding, interoperability, and multi-user skills.

This article shows you how to use the Windows 32-bit application programming interface (API) ShellExecute() function to start an application associated with a given file extension without having to know the name of the associated application. For example, you can start Microsoft Paint by passing the file name Bubbles.bmp to the ShellExecute() function. Or, you can connect to the World Wide Web (by using a Web browser installed on your computer) by passing a hyperlink or URL (Uniform Resource Locator) to the API function.

NOTE: Microsoft Access 97 has this functionality built in.

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to the "Building Applications with Microsoft Access for Windows 95" manual.

MORE INFORMATION

To use the Windows API ShellExecute(), you must first declare the function in a standard Visual Basic for Applications module. After you have declared the function, you can use the function by following one of the two examples described later in this article.

Declaring the Windows API ShellExecute()

1. Open the sample database Northwind.mdb and create a new module named

   Module1.

2. Add the following code to the Declarations section:

      Option Explicit

      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

      Global Const SW_SHOWNORMAL = 1

3. Close and save Module1.

To test the ShellExecute() function, use one of the following examples.

Example 1: How to Connect to the World Wide Web

NOTE: This functionality is built in to Microsoft Access 97.

1. Create a new table with the following structure:

      Table: WebSites
      -------------------------------
      FieldName: SiteID
         DataType : AutoNumber
         Indexed: Yes (No Duplicates)
      FieldName: SiteURL
         DataType : Text

2. Save the table as WebSites and switch the table to Datasheet view. Enter
   the following three records:

      SiteID   SiteURL
      ----------------------------------------
      1        ftp.microsoft.com
      2        www.microsoft.com/kb.htm
      3        http://www.microsoft.com/devonly

3. Use the AutoForm: Columnar Wizard to create a new form based on the
   WebSites table. 

4. Switch the form to Design view and add the following command button:

      Command button:
         Name: cmdConnect
         Caption: Connect to Web

5. Set the cmdConnect button's OnClick property to the following event
   procedure:

      Private Sub cmdConnect_Click()
      On Error GoTo cmdConnect_Click_Error

         Dim StartDoc As Long
            If Not IsNull(Me!SiteURL) Then
              StartDoc = ShellExecute(Me.Hwnd, "open", Me!SiteURL, _
                "", "C:\", SW_SHOWNORMAL)
            End If

         Exit Sub

      cmdConnect_Click_Error:
         MsgBox "Error: " & Err & " " & Error
         Exit Sub
      End Sub

6. Switch the form to Form view.

7. Click the Connect To Web button. Note that your Web browser is started

   automatically and displays the Web site for the URL listed in the
   current record.

Example 2: How to Open a File in Its Associated Application

1. Open Module1 and create the following procedure:

      Function StartDoc (DocName As String)
      On Error GoTo StartDoc_Error

        StartDoc = ShellExecute(Application.hWndAccessApp, "Open", DocName,
            _ "", "C:\", SW_SHOWNORMAL)
      Exit Function

      StartDoc_Error:
         MsgBox "Error: " & Err & " " & Error
         Exit Function
      End Function

2. On the View menu, click Debug Window.

3. In the Debug window, type the following line, and then press ENTER:

      StartDoc "Bubbles.bmp"

   Note that the function starts Microsoft Paint, which loads the
   Bubbles.bmp file.

NOTES

REFERENCES

For information about using the Windows 16-bit API ShellExecute() function in Microsoft Access version 2.0, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q121157
   TITLE     : ACC: How to Start Doc with Windows API ShellExecute()
               Function

For more information about the ShellExecute() function, please see the Microsoft Win32 SDK "Programmer's Reference."

Additional query words:

Keywords          : kbprg
Version           : 7.0 97
Platform          : WINDOWS
Hardware          : x86
Issue type        : kbhowto

Last Reviewed: November 20, 1998