Hi. In the following procedure that I found in an example of using classes I am trying to cause an error so that the class code below auotmatically protects a worksheet that my code has unprotected, but this doesn't happen as Class_Terminate isn't run. What is Class_Terminate and why doesn't this work?
Public Sub DoSomething()
Dim objWorksheetProtector As WorkSheetProtector
Set objWorksheetProtector = New WorkSheetProtector
objWorksheetProtector.Unprotect Application.ActiveWorkbook.ActiveSheet, "password"
'... manipulate myWorksheet - may raise an error
Application.ActiveWorkbook.Name = "?"
'--- WorksheetProtector class module ---
Private m_objWorksheet As Worksheet
Private m_sPassword As String
Public Sub Unprotect(Worksheet As Worksheet, Password As String)
' Nothing to do if we didn't define a password for the worksheet
If Len(Password) = 0 Then Exit Sub
' If the worksheet is already unprotected, nothing to do
If Not Worksheet.ProtectContents Then Exit Sub
' Unprotect the worksheet
' Remember the worksheet and password so we can protect again
Set m_objWorksheet = Worksheet
m_sPassword = Password
Public Sub Protect()
' Protects the worksheet with the same password used to unprotect it
If m_objWorksheet Is Nothing Then Exit Sub
If Len(m_sPassword) = 0 Then Exit Sub
' If the worksheet is already protected, nothing to do
If m_objWorksheet.ProtectContents Then Exit Sub
Set m_objWorksheet = Nothing
m_sPassword = ""
Private Sub Class_Terminate()
' Reprotect the worksheet when this object goes out of scope
On Error Resume Next