Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

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


View products that this article applies to.

This article was previously published under Q210096
Advanced: Requires expert coding, interoperability, and multiuser skills.

This article applies only to a Microsoft Access database (.mdb).

↑ Back to the top


Summary

This article describes 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 Uniform Resource Locator (URL) to the API function.

↑ Back to the top


More information

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. To use the Windows API ShellExecute() function, you must first declare the function in a standard Visual Basic for Applications module. After you declare the function, you can use the function by following one of the two examples described later in this article.

Declaring the Windows API ShellExecute()

CAUTION: If you follow the steps in this example, you modify the sample database Northwind.mdb. You may want to back up the Northwind.mdb file and follow these steps on a copy of the database.

  1. Start Microsoft Access and 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 Open a Location on the World Wide Web

NOTE: This functionality is built into Microsoft Access 2000.
  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 open the table in Datasheet view. Type the following three records:
       SiteID   SiteURL
       ----------------------------------------
         1      ftp.microsoft.com
         2      www.microsoft.com/kb.htm  
         3      http://www.microsoft.com/devonly
    					
  3. In the Database window, click Forms, and then click New.
  4. In the New Form dialog box, click AutoForm: Columnar and select WebSites as the table where the object's data comes from. Click OK.
  5. On the View menu, click Design View. Add the following command button to the Detail section of the form:
    Command button
    Name: cmdConnect
    Caption: Connect to Web
  6. Set the On Click property of the cmdConnect button 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
    					
  7. On the View menu, click Form View.
  8. 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. In the Immediate 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.

↑ Back to the top


Keywords: KB210096, kbprogramming, kbhowto

↑ Back to the top

Article Info
Article ID : 210096
Revision : 3
Created on : 6/23/2005
Published on : 6/23/2005
Exists online : False
Views : 417