ACC95: GetObject Sample Function Does Not Detect Excel 7.0

Last reviewed: August 29, 1997
Article ID: Q155201
The information in this article applies to:
  • Microsoft Access version 7.0

SUMMARY

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

The GetObject Function Example in online Help for Microsoft Access for Windows 95 states that you can use GetObject to test if a copy of Microsoft Excel is already running. This works fine with versions of Microsoft Excel earlier than version 7.0. However, because Microsoft Excel version 7.0 does not register itself in the running object table (ROT) by default on startup, you must use a combination of Windows application programming interface (API) functions and GetObject to be certain to detect an instance of Microsoft Excel 7.0.

The following is an excerpt from the GetObject Function Example in Microsoft Access for Windows 95 online Help:

   ' Test to see if there is a copy of Microsoft Excel already running.
   On Error Resume Next   ' Defer error trapping.
   ' GetObject function called without the first argument returns a
   ' reference to an instance of the application. If the application
   ' is not running, an error occurs. Note the comma used as the first
   ' argument placeholder.

   Set MyXL = GetObject(, "Excel.Application")

Notice that the comment states that Set MyXL = GetObject(, "Excel.Application") will detect whether there is an instance of Excel already open.

MORE INFORMATION

In versions of Microsoft Excel prior to version 7.0, Excel was automatically registered in the ROT when it opened. Microsoft Excel 7.0 doesn't automatically register in the ROT by default on startup. The following sample code uses Windows API functions to detect if Microsoft Excel 7.0 is running and to register it in the ROT.

  1. Create a module and type the following lines in the Declarations section:

          Option Explicit
    

          Const WM_USER = 1024
          Declare Function FindWindow Lib "user32" Alias _
    
             "FindWindowA" (ByVal lpClassName As String, ByVal _
             lpWindowName As Long) As Long
    
          Declare Function SendMessage Lib "user32" Alias _
             "SendMessageA" (ByVal hwnd As Long, ByVal wMsg _
             As Long, ByVal wParam As Long, lParam As Long) As Long
    
    

  2. Type the following procedure:

          Sub DetectExcel()
          '=================================================================
          'This procedure detects if Excel7 is running and registers it.
          '=================================================================
          '
          Dim hwnd As Long
          'Find the windows handle for Excel.
          'This returns a valid handle number,
          'or zero if Excel is not running
          hwnd = FindWindow("XLMAIN", 0)
          If hwnd = 0 Then
             'Excel is not running
             Exit Sub
          End If
          'Excel is running
          'Registers Excel in the ROT
          SendMessage hwnd, WM_USER + 18, 0, 0
          End Sub
    
    
In the GetObject Function Example in Microsoft Access for Windows 95 online Help, you can call the DetectExcel function before you run the GetObject procedure to be certain Microsoft Excel 7.0 is detected. For example, you can change the Help excerpt referenced earlier to:

   ' Test to see if there is a copy of Microsoft Excel already running.
   On Error Resume Next   ' Defer error trapping.
   ' GetObject function called without the first argument returns a
   ' reference to an instance of the application. If the application
   ' is not running, an error occurs. Note the comma used as the first
   ' argument placeholder.
   DetectExcel
   Set MyXL = GetObject(, "Excel.Application")

REFERENCES

For more information about the GetObject function, search on the phrase "GetObject," and then view "GetObject Function" using the Answer Wizard from the Microsoft Access for Windows 95 Help menu.

For more information about GetObject not detecting Microsoft Excel Version 7.0, please see the following articles in the Microsoft Knowledge Base:

   ARTICLE-ID: Q138723
   TITLE     : Code to Access MS Excel Doesn't Work in Version 7.0

   ARTICLE-ID: Q153029
   TITLE     : Microsoft Excel 95 Doesn't Respond Correctly to GetObject


Additional query words: started open
Keywords : AutoGnrl DcmHlp kbinterop kbprg IntpOleA
Technology : kbole
Version : 7.0
Platform : WINDOWS
Hardware : x86
Issue type : kbdocerr


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 29, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.