XL2000: Crash Running Macro When Object Set to Nothing (235107)



The information in this article applies to:

  • Microsoft Excel 2000

This article was previously published under Q235107

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.

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.

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
				

STATUS

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

MORE INFORMATION

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

Modification Type:MinorLast Reviewed:10/11/2006
Keywords:kbbug kbdtacode kberrmsg kbpending KB235107