Persistent Application level Events after user resetting variables ?

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
9,616
Office Version
  1. 2016
Platform
  1. Windows
Hi all.

Here is the deal - Suppose the application is hooked upon opening the workbook like so :

Code:
Private WithEvents AppEvents As Application

Private Sub Workbook_Open()
 
    Set AppEvents = Application
 
End Sub

Private Sub AppEvents_NewWorkbook(ByVal Wb As Workbook)
 
    MsgBox "hello!"
    
End Sub

This should keep the application hooked as long as the AppEvents Variable points to the application object. However if the Project is reset by the user or simply some code accidently errors out , this will inforce the reinitialization of all variables including the AppEvents variable and obviously the application will cease to be hooked.

Any ideas on how to work around this and make the application's events persistent throughout ?

Regards.
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
WORKBOOK DEMO.

Here is a workaround that seems to actually work. Basically, by declaring the WithEvents AppEvents Workbook Property as Public or wrapping it in a Public Function, one could access it from outside the excel process using automation.

The code below creates an out of process VBS file on the fly that runs a continious loop within which the AppEvents Property is constantly being assigned the application object thus allowing the excel events to persist until the workbook is closed even if the the VBIDE is reset during the excel session either by accident (ie the code erroring out during execution) or by editing/debugging the code.

This hack can also be easily amended to work for other Class based events ie ( Commandbars, ActiveX controls etc )

I was hoping to achieve this by blocking the memory assigned to the AppEvents variable using some memory API functions but I am not even sure if that is possible. That would certainly be more elegant and a good learning exercise.


Code in the WorkBook Module :

Code:
Option Explicit
 
Public WithEvents AppEvents As Application
Private Const PERSISTENT_HOOK_FILE As String = "C:\PersistentHook.vbs"
 
Private Sub AppEvents_SheetSelectionChange _
(ByVal Sh As Object, ByVal Target As Range)
 
    MsgBox "Excel remains hooked after reseting the VBIDE ! "
 
End Sub
 
Private Sub Workbook_Open()
 
    Call PersistentHook
 
End Sub
 
Private Sub Workbook_BeforeClose(Cancel As Boolean)
 
    Kill PERSISTENT_HOOK_FILE
 
End Sub
 
Private Sub PersistentHook()
 
    Open PERSISTENT_HOOK_FILE For Output As #1
 
        Print #1, "Dim oWb"
        Print #1, "On Error Resume Next"
        Print #1, _
        "Set oWb = Getobject(""" & ThisWorkbook.FullName & """)"
        Print #1, "Do"
        Print #1, "Set oWb.AppEvents = oWb.Parent"
        Print #1, "Loop Until oWb.Parent.ActiveCell Is Nothing"
        Print #1, "Set oWb = Nothing"
 
    Close #1
 
    Shell "WScript.exe " & PERSISTENT_HOOK_FILE
End Sub

Regards.
 
Upvote 0
Jaafar -

I've not found some decent docs which explain the constructs used in your code so I'm somewhat clueless as to what its doing.

I do have a code workbook and it works great and opens and closes many different workbooks which hold user data and behaves as expected.

In your example, it assumes this code resides in the Workbook object but I'm not able to place there as the code for application resides in a seperate workbook.

Perhaps you could provide some guidance either to some docs or an explanation.

Many thanks in advance.

- Bill
 
Upvote 0
Jaafar -

I've not found some decent docs which explain the constructs used in your code so I'm somewhat clueless as to what its doing.

I do have a code workbook and it works great and opens and closes many different workbooks which hold user data and behaves as expected.

In your example, it assumes this code resides in the Workbook object but I'm not able to place there as the code for application resides in a seperate workbook.

Perhaps you could provide some guidance either to some docs or an explanation.

Many thanks in advance.

- Bill

Hi Bill.

I am not sure I understand the problem you are having but what the code does is to ensure the application level events are always working even if the VBProject is reset and the the variables are reinitialized.

If the code you have resides in a seperate workbook you will need to put the code in that workbook module. Alternatively you could place the code in the Personal.xls workbook or an addin so it is always running each time you open Excel.
 
Upvote 0

Forum statistics

Threads
1,214,925
Messages
6,122,298
Members
449,077
Latest member
Rkmenon

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top