PRB: Releasing Object Variable Does Not Close Microsoft Excel

Last reviewed: December 31, 1996
Article ID: Q132535
The information in this article applies to:
  • Microsoft Visual FoxPro for Windows, version 3.0

SYMPTOMS

The CREATEOBJECT function can be used to create a reference to an instance of 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 of error messages, depending on the machine configuration and Windows version. These error messages include:

   "Insufficient Memory"
   "Not enough memory"
   "Page Fault"

RESOLUTION

If you are running Microsoft Excel 5.0, use the following code to close all instances 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 of a change in behavior between Microsoft Excel 7.0 and Microsoft Excel 5.x. For more information, please see the following article in the Microsoft Knowledge Base.

   ARTICLE-ID: Q147573
   TITLE     : PRB: Microsoft Excel Not Registered in Running Object Table

NOTE: The DECLARE statements in the above example are case-sensitive and the functions must be called just as in the example.

STATUS

This behavior is by design.

MORE INFORMATION

Steps to Reproduce Behavior

Run the following code to create five instances of Microsoft Excel, none of which are visible:

   FOR i = 1 to 5
     x = CREATEOBJECT("Excel.Application")
     RELEASE x
   ENDFOR

Releasing the variable x does not terminate the instance of Microsoft Excel. To ensure that each instance is terminated, add the following command immediately before the RELEASE x command:

   x.Quit

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.

REFERENCES

For more information about the GETOBJECT function, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q128994
   TITLE     : Behavior of GETOBJECT() with Excel and Word for Windows


Additional reference words: 3.00 VFoxWin
KBCategory: kbinterop kbole kbprb
KBSubcategory: FxinteropExcel


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: December 31, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.