PRB: Releasing Object Variable Does Not Close Microsoft Excel

This article was previously published under Q132535
This article has been archived. It is offered "as is" and will no longer be updated.
The CREATEOBJECT function can be used to create a reference to an instanceof Microsoft Excel and assign that reference to a variable. However,releasing that variable does not cause the instance of Excel to quit.

Inadvertently creating multiple instances of Excel can cause a variety oferror messages, depending on the machine configuration and Windows version.These error messages include:
"Insufficient Memory"
"Not enough memory"
"Page Fault"
If you are running Microsoft Excel 5.0, use the following code to close allinstances of Microsoft Excel:
   PROCEDURE xlquit   local llFlag   ON ERROR  llFlag = .F. && Exit loop   llFlag = .T.   DO  WHILE  llFlag      y=GETOBJECT (,"Excel.Application")      y.QUIT   ENDDO   ON ERROR   && Set back to default				
If you are using Microsoft Excel 7.0, use the following code:
   PROCEDURE xlquit   DECLARE LONG FindWindowA IN USER32 AS FindA STRING,STRING   DECLARE LONG SendMessageA IN USER32 AS SendA LONG, LONG, LONG, LONG   WM_USER = 1024   hwnd = FindA("XLMAIN", 0)     DO  WHILE  hwnd > 0       WhatD= SendA(hwnd, WM_USER + 18, 0, 0)       y=GETOBJECT (,"Excel.Application")       y.QUIT       hwnd = FindA("XLMAIN", 0)     ENDDO				
The code is different because Microsoft Excel 95 contains a bug thatwas corrected in later versions.

If you are using Microsoft Office 2000 or Microsoft Office XP change the following code:
WhatD= SendA(hwnd, WM_USER + 18, 0, 0)
to the following:
WhatD= SendA(hwnd, WM_ACTIVATEAPP, 0, 0)

For additional information, click the article number below to view the article in the Microsoft Knowledge Base:
153025 FIX: Microsoft Excel 95 Doesn't Respond Correctly toGetObject
NOTE: The DECLARE statements in the above example are case-sensitive andthe functions must be called just as in the example.
This behavior is by design.

Steps to Reproduce Behavior

Run the following code to create five instances of Microsoft Excel, none ofwhich are visible:
   FOR i = 1 to 5     x = CREATEOBJECT("Excel.Application")     RELEASE x   ENDFOR				
Releasing the variable x does not terminate the instance of MicrosoftExcel. To ensure that each instance is terminated, add the followingcommand immediately before the RELEASE x command:
To test if an instance of Microsoft Excel exists use this function:
   x=GETOBJECT(,"Excel.Application")  && The first argument is empty				
This returns an OLE error if no instance of Microsoft Excel is in memory.

Article ID: 132535 - Last Review: 02/28/2014 07:57:21 - Revision: 2.1

  • Microsoft Visual FoxPro 3.0 Standard Edition
  • Microsoft Visual FoxPro 5.0 Standard Edition
  • Microsoft Visual FoxPro 5.0a
  • Microsoft Visual FoxPro 6.0 Professional Edition
  • kbnosurvey kbarchive kbprb KB132535