I try to automate as much of my coding as possible so that I don't have to remember to do something. The easiest way I have discovered is to create objects with the code that I need to remember in the Class_Terminate().
In this example I turn EnableEvents = False in a class object. The wrapper sub (EEOff()) does not create an object unless EnableEvents=True. When object oEE goes out of scope it automatically turns EnableEvents back on. So I don't have to worry that I might have forgotten to turn it back on.
Sub mySub()
Dim oEE as Object _
, o2 as Object
Set oEE = EEOff()
Set o2 = myObj2()
' more code
End Sub
Function EEOff() as Object
If Application.EnableEvents then
Set EEOff = New Cls_EEOff
End If
End Function
'-----------------------------
'cls_EEOff code
Sub Class_Initialize
Set Application.EnableEvents=False
End Sub
Sub Class_Terminate
Set Application.EnableEvents=True
End Sub
Every now and then the objects need to go out of scope in a particular order. In my example, I might need o2 to execute it's Class_Terminate before oEE executes it's Class_Terminate. Unfortunately they go out of scope in the wrong order, so I have to specifically remember to "Set o2 = Nothing" before the sub ends, which is what I'm trying to avoid.
Sub mySub()
Dim oEE as Object _
, o2 as Object
Set oEE = EEOff()
Set o2 = myObj2()
' more code
Set o2 = Nothing
End Sub
Any suggestions on how to accomplish what I want?
Bookmarks