XL7: Code to Access MS Excel Doesn't Work in Version 7.0Last reviewed: September 2, 1997Article ID: Q138723 |
The information in this article applies to:
SYMPTOMSIn 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.
CAUSEEarlier 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.
STATUSThis change in Microsoft Excel 7.0 is by design.
MORE INFORMATIONMicrosoft Excel 7.0 DOES register itself in the ROT in the following situations:
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, 0Set 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
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |