Trapping Application events in Excel

To be able to trap application events across all open workbooks in MS Excel via an add-in follow these steps.

  1. Add a class module
    In the class module enter the following at the top of the module
    Public WithEvents xlApp as Application Then you will be able to write code within any of the application events.
  2. Add a global variable in a standard module of the class type created in 1.
  3. In your initialisation code for the Add-in, call a routine to initialize the class application object
    eg.

    Sub TrapApplicationEvents()
       set globalvar = new classApp
       set globalvar.xlApp = Application
    End Sub