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

Last reviewed: August 28, 1997
Article ID: Q148632
The information in this article applies to:
  • Microsoft Access versions 7.0, 97

SUMMARY

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

This article demonstrates how you can 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 (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

The first step in using the Windows API ShellExecute() is to declare the function in a standard Visual Basic for Applications module. Once 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, follow one of the examples described below.

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. Create a new form based on the WebSites table using the AutoForm: Columnar Wizard.

  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

  • The Windows API ShellExecute() function differs from the Visual Basic Shell() function in that you can pass the ShellExecute() function the name of a document, and it will start the associated application, and then pass the file name to the application. You can also specify the working folder for the application.
  • The return value for the StartDoc() function is the same as for the Shell() function. It is the Windows instance value of the application that was started.

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."
Keywords          : IntpShell kbprg PgmApi
Version           : 7.0 97
Platform          : WINDOWS
Hardware          : x86
Issue type        : kbhowto


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: August 28, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.