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: Crash Running Macro When Object Set to Nothing


View products that this article applies to.

This article was previously published under Q235107

↑ Back to the top


Symptoms

In Microsoft Excel, when you run a Microsoft Visual Basic for Applications macro, you may receive the following error message:
This program has performed an illegal operation and will be shut down.
If you click Details, you receive an error message similar to the following:
EXCEL caused an invalid page fault in module EXCEL.EXE at 015f:300603c4.
This may be followed by a second illegal operation error message similar to the following:
EXCEL caused an invalid page fault in module EXCEL.EXE at 015f:300149b0.
NOTE: You may receive this error message even though the procedure ran successfully in earlier versions of Microsoft Excel.

↑ Back to the top


Cause

This problem may occur if all of the following conditions are true:
  • You create an object variable in a Visual Basic for Applications macro and assign it to an object.

    -and-

  • When you are finished using the object variable, you set the object variable to the Nothing keyword, so the variable no longer refers to the actual object.

    -and-

  • You run the macro.
NOTE: If you step through the macro, it does not crash.

↑ Back to the top


Resolution

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. To prevent this problem, explicitly close an object with the Close method before setting an object variable to Nothing.

Example:

The following code may generate the errors shown in the "Symptoms" section:
Sub Test()
Set x = CreateObject("Excel.Sheet")

Set y = Workbooks.Add
x.Activate

Debug.Print y.Author

Set x = Nothing
End Sub
				
Replace this with:
Sub Test()
Set x = CreateObject("Excel.Sheet")

Set y = Workbooks.Add
x.Activate

Debug.Print y.Author
x.Close
Set x = Nothing
End Sub
				

↑ 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


More information

It is generally good programming practice to close an object you have created before destroying it.

↑ Back to the top


Keywords: KB235107, kbpending, kbdtacode, kbbug, kberrmsg

↑ Back to the top

Article Info
Article ID : 235107
Revision : 6
Created on : 10/11/2006
Published on : 10/11/2006
Exists online : False
Views : 212