Steps to Reproduce Behavior
- In Visual Basic, create a new ActiveX DLL project. Change the project name to ExcelTest.
- Change the name of Class1 to ParentClass and then add the following code to ParentClass:
Option Explicit
Private oChild As ChildClass
Private WorkbookRef As Object
Private Sub Class_Initialize()
Set oChild = New ChildClass
Set oChild.Parent = Me
End Sub
Private Sub Class_Terminate()
Set oChild.WorkbookRef = Nothing
Set oChild.Parent = Nothing
Set oChild = Nothing
MsgBox "ParentClass Terminate Event"
End Sub
Public Sub Clear()
Set oChild.Parent = Nothing
End Sub
Public Sub SetWorkbook(o As Object)
Set WorkbookRef = o
Set oChild.WorkbookRef = o
End Sub
- Add another class module, name it ChildClass, and then add the following code to ChildClass:
Public Parent As ParentClass
Public WorkbookRef As Object
- Build "ExcelTest.dll".
- Create a new workbook in Microsoft Excel. Press ALT+F11 to
open the Visual Basic Editor.
- From the Insert menu, click UserForm to add a new userform to the VBA project.
- From the Insert menu, click Module to add a new module to the VBA project. Add the following code to
the new module:
Public o As Object
Sub MyMacro()
UserForm1.Show
Set o = CreateObject("ExcelTest.ParentClass")
o.SetWorkbook ThisWorkbook
'o.Clear '<=== Remove comment to demonstrate the workaround.
Set o = Nothing
End Sub
- From the Tools menu, select VBAProject Properties. On the dialog box that appears, click the Protection tab. Click to select Lock Project for Viewing. Supply a password and click OK.
- Save the workbook and close it.
- Now, to reproduce the problem with the VBA project password
appearing when Excel quits:
- Open the workbook that you saved in step 9.
In Microsoft Office Excel 2007, if you are prompted by a security warning that indicates that macros have been disabled, click Options. In the Security dialog box, click Enable this content, and then click OK.
- Perform one of the following actions:
- In Excel 2007, click Macros in the Code group on the Developer tab. If the Developer tab does not a[[ear, click the Microsoft Office Button, click Excel Options, click Popular, click to select the Show Developer Tab in the Ribbon check box, and then click OK.
- In Microsoft Office Excel 2003 or in an earlier version of Excel, click Macro on the Tools menu, and then click Macros.
- Select MyMacro in the list, and then click Run.
- Dismiss the userform that the macro
displays.
- Quit Microsoft Excel.
- You are prompted for the VBA project password when
Excel quits.
When you use the steps above to reproduce the problem, note
that the
Terminate event for the
ParentClass class does not fire even after setting its object in the Excel
macro to
Nothing. (The absence of the MessageBox with the message "ParentClass
Terminate Event" indicates that this event did not fire.)
To correct
the problem so that the reference to the Excel workbook is properly released,
remove the comment from the line designated in the Excel macro, save the macro,
and then repeat the test. When the workbook reference is properly released, the
object's
Terminate event will fire and you will no longer be prompted for the VBA
project password when Excel quits.