ACC: How to Start Doc with Windows API ShellExecute() Function

Last reviewed: June 8, 1997
Article ID: Q121157
The information in this article applies to:
  • Microsoft Access versions 1.0, 1.1, 2.0

SUMMARY

Advanced: Requires expert coding, interoperability, and multiuser skills.

You can use the Windows application programming interface (API) ShellExecute() function to start the application associated with a given document extension without knowing the name of the associated application. For example, you can start the Microsoft Paintbrush program by passing the file name ARCADE.BMP to the ShellExecute() function.

MORE INFORMATION

The following example demonstrates how to start an application or load a document into its associated application. The Windows API ShellExecute() function is different from the Access 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 filename to the application. You can also specify the working directory for the application.

NOTE: In the following sample code, an underscore (_) at the end of a line is used as a line-continuation character. Remove the underscore from the end of the line when re-creating this code in Access Basic.

  1. Create a new module, and then enter the following lines in the module's Declarations section:

          Option Explicit
          Declare Function ShellExecute Lib "SHELL" (ByVal hwnd%, ByVal _
          lpszOp$, ByVal lpszFile$, ByVal lpszParams$, ByVal lpszDir$, _
          ByVal fsShowCmd%) As Integer
          Declare Function GetDesktopWindow Lib "USER" () As Integer
    

  2. Create the following procedure in the module:

          Function StartDoc (DocName As String)
    
             StartDoc = ShellExecute(GetDesktopWindow(), "Open", DocName, _
              "", "C:\", 1)
          End Function
    
    

  3. From the View menu, choose Immediate Window.

  4. In the Immediate window, type the following line and then press ENTER:

          ? StartDoc("ARCADE.BMP")
    

    Note that the function starts the Paintbrush program, which will then load the ARCADE.BMP file.

NOTES

  • 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.
  • The ShellExecute() function returns the value 31 if there is no association for the specified file type or if there is no association for the specified action within the file type. Other error values are:

          Error Value   Meaning
          ----------------------------------------------------------------
               2        The file was not found.
               3        The path was not found.
               8        There was insufficient memory to start the
                        application.
              10        The Windows version was incorrect.
              11        The executable file was invalid. Either it was not
                        a Windows application or there was an error in the
                        .EXE image.
    
    

    REFERENCES

    For an example of how to use the Windows application programming interface (API) ShellExecute() function to start the application associated with a given document extension without knowing the name of the associated application in Microsoft Access for Windows 95 version 7.0, please see the following article in the Microsoft Knowledge Base:

       ARTICLE-ID: Q148632
       TITLE:      ACC: Start Files or Hyperlinks with Windows API
                   ShellExecute()
    
    
Microsoft Windows SDK "Programmer's Reference, Volume 2: Functions," pages 901-904


Keywords : kbprg PgmApi
Version : 1.0 1.1 2.0
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: June 8, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.