XL7: Code to Access MS Excel Doesn't Work in Version 7.0

Last reviewed: September 2, 1997
Article ID: Q138723
The information in this article applies to:
  • Microsoft Excel for Windows 95, version 7.0

SYMPTOMS

In earlier versions of Microsoft Excel, you can create Visual Basic or C/C++ code to allow another application to access a currently running instance of Microsoft Excel. To do this, you use code similar to the code in the following examples:

   VB Code Example
   ---------------

   Dim myExcelApp As Object
   Set myExcelApp = GetObject(, "Excel.Application")

   C/C++ Code Example
   ------------------

   LPOLESTR   lpszProgID = OLESTR("Excel.Application");
   LPUNKNOWN pUnk;
   if (FAILED(CLSIDFromProgID(lpszProgID, &clsid)))
      return;
   HRESULT hr =GetActiveObject(clsid,NULL,pUnk);

This code will not work with Microsoft Excel version 7.0, even if Microsoft Excel is running.

CAUSE

Earlier versions of Microsoft Excel register the application object in the OLE RunningObjectTable (ROT) on startup. This registration occurs under all circumstances, regardless of whether Microsoft Excel is started by OLE. However, Microsoft Excel 7.0 does not register the application object in the ROT on startup by default.

STATUS

This change in Microsoft Excel 7.0 is by design.

MORE INFORMATION

Microsoft Excel 7.0 DOES register itself in the ROT in the following situations:

  • When it's started by OLE; that is, when a client calls CoCreateInstance(), CoGetClassObject(), or CreateInstance().
  • When a module is inserted in one of the workbooks.
  • When a WM_USER+18 message is sent to the Main window of Microsoft Excel.

The following VB 4.0 and C/C++ code samples show how to use the WM_USER+18 option to get an OLE Automation object from an already running instance of Excel.

Sample VB Code

Private Declare Function FindWindowA lib "User32" (byval sClass as String,
byval xTitle as long) as long

Private Declare Function SendMessageA lib "User32" (byval hwnd as long,
byval msg as long, byval wParam as long, byval lParam as long) as long

Private const WM_USER = 1024 Dim myExcelApp As Object

sub KickExcel()
   dim hwnd as long

   hwnd = FindWindowA("XLMAIN", 0)
   if hwnd = 0 then
      msgbox "No instances of Excel running?"
      exit sub
   endif
   SendMessageA hwnd, WM_USER + 18, 0, 0
Set myExcelApp = GetObject(, "Excel.Application") end sub

Sample C/C++ Code

LPOLESTR   lpszProgID = OLESTR("Excel.Application");
LPUKNOWN pUnk; HWND hExcelMainWnd =0; hExcelMainWnd = FindWindow("XLMAIN",NULL); if(!hExcelMainWnd)
   MessageBox(NULL,"No instances of Excel running?","Error",MB_OK);
SendMessage(hExcelMainWnd,WM_USER + 18, 0, 0); if (FAILED(CLSIDFromProgID(lpszProgID, &clsid)))
   return;
HRESULT hr =GetActiveObject(clsid,NULL,pUnk);


Additional query words: kbinf launched
Keywords : kbprg PgmHowTo kbcode kbole kbprg
Version : 7.00
Platform : WINDOWS


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