XL7: Code to Access MS Excel Does Not Work in Version 7.0
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 : kbExcel
Version : WINDOWS:7.0
Platform : WINDOWS
Issue type :