Automation Error Using Excel Object in Visual Basic Procedure

Last reviewed: September 2, 1997
Article ID: Q134835
The information in this article applies to:
  • Microsoft Excel for Windows, versions 5.0, 5.0c
  • Microsoft Excel for Windows 95, version 7.0
  • Standard and Professional Editions of Microsoft Visual Basic Programming System for Windows, version 3.0

SYMPTOMS

When you use a Visual Basic application to create an OLE Automation object using Microsoft Excel, you may receive the following error message when you attempt to access the object:

   OLE Automation error

CAUSE

This problem occurs when you access a Microsoft Excel OLE Automation object in a Visual Basic procedure while Microsoft Excel is closed. If, for example, you create a reference for a worksheet object using the CreateObject function, and you create a reference for another Microsoft Excel object using the GetObject function in your procedure, if you then set the Microsoft Excel worksheet object (Excel.Sheet) equal to nothing, you receive the OLE Automation error message if you then try to access the other object.

This problem occurs because setting a Microsoft Excel object that was created using the CreateObject function equal to Nothing closes Microsoft Excel, even if your Visual Basic procedure still has a reference to another Microsoft Excel object.

For example, you receive this error message when you run the following code in Microsoft Visual Basic:

   Dim xlSheet As Object
   Dim xlApp As Object
   Set xlSheet = CreateObject("Excel.Sheet")
   MsgBox xlSheet.Application.Name
   Set xlApp = GetObject(, "Excel.Application")
   MsgBox xlApp.Name
   Set xlSheet = Nothing
   MsgBox xlApp.Name

You receive this error message because the statement "Set xlSheet = Nothing" closes Microsoft Excel, and the "MsgBox xlApp.Name" statement that follows in the procedure attempts to access the Microsoft Excel application object again.

Note that in the above example, if you set the Microsoft Excel application object equal to nothing (Set xlApp = Nothing), you do not receive an error message if you then access the Microsoft Excel worksheet object (xlSheet) in the procedure. Additionally, you do not receive this error message if Microsoft Excel is running when you run this macro because, in this case, the CreateObject function starts another instance of Microsoft Excel.

RESOLUTION

To avoid this behavior in a Visual Basic procedure, do not set the value of a Microsoft Excel object that was created using the CreateObject function equal to Nothing until you are done accessing all Microsoft Excel OLE Automation objects in the procedure.


Additional query words: 7.00 5.00 5.00c
Keywords : AutoGnrl kbinterop kbprg xlwin
Version : 5.00 5.00c 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.