Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

XL2000: Automation Doesn't Release Excel Object from Memory


View products that this article applies to.

Symptoms

When you run a macro that uses automation to create a Microsoft Excel object (instance), the Excel object does not exit from memory when you specify. If you create another Excel object after quitting the first, a second instance is in memory.

↑ Back to the top


Cause

This problem occurs when your macro uses a With statement that refers to the automation object.

↑ Back to the top


Workaround

When you create an Excel automation object using the CreateObject function, the automation object remains in memory until you set it equal to the keyword Nothing. However, when you reference the object using a With statement, the object remains in memory until the subroutine ends. In order to control the release of the Excel automation object from memory, do not use the With statement when referring to the Excel object. Instead, explicitly refer to the object by its variable name. For example, with an Excel automation object named myExcel, the following statements illustrate the preferred way to refer to the automation object:

Use this:
   myExcel.Workbooks(1).Worksheets(1).Cells(1,1).Value = "Hello"
   myExcel.Workbooks(1).Worksheets(1).Cells(2,1).Value = "World"
				

Instead of:
   With myExcel.Workbooks(1).Worksheets(1)
      .Cells(1,1).Value = "Hello"
      .Cells(2,1).Value = "World"
   End With
				

↑ Back to the top


Status

Microsoft has confirmed that this is a problem in the Microsoft products that are listed at the beginning of this article.

↑ Back to the top


References

For more information about the CreateObject function, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type CreateObject function in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

↑ Back to the top


Keywords: KB199219, kbprb

↑ Back to the top

Article Info
Article ID : 199219
Revision : 3
Created on : 10/7/2003
Published on : 10/7/2003
Exists online : False
Views : 204