How to intercept events workbook/xls/xlsm document closed/opened ?

Skip to first unread message


Apr 20, 2022, 11:35:28 AMApr 20
to Excel-DNA

I am trying to dispose some Shapes objects on close/open excel document. How can I intercept those events?



Mohummed Ibrahim

Apr 20, 2022, 11:54:15 AMApr 20
The Excel Application object also has those events WorkbookOpen and WorkbookBeforeClose you can subscribe to.
i.e. something like this.
xlApp.WorkbookOpen += (wb) => Debug.WriteLine(wb.Name + "is open.");
xlApp.WorkbookBeforeClose += (wb, cancel) => Debug.WriteLine(wb.Name + "is going to be closed.");

You received this message because you are subscribed to the Google Groups "Excel-DNA" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To view this discussion on the web visit

Kedar Kulkarni

Apr 20, 2022, 12:32:45 PMApr 20
to Excel-DNA
just a word of caution - the before close event could be called multiple times for the same workbook ie if a workbook has unsaved changes- then after the workbook before close event, a dialog is shown to save Yes No Cancel dialog and its possible that user presses cancel. So it could be a problem to dispose shapes in before close event. 

there is a hacky way to identify workbook after close event. - one could remember the count of workbooks in the workbook deactivate event (keep the collection of things to dispose handy) AND in the workbook activate event check if the workbook count has reduced - if yes dispose the collection else clear the collection. (btw if only one workbook is open then the workbook deactivate event would be fired when that workbook is closed - there is no other workbook to activate here..)

Hope that helps.


Apr 20, 2022, 1:46:25 PMApr 20
to Excel-DNA
Thanks a lot, my friend.

On Wednesday, April 20, 2022 at 11:54:15 AM UTC-4 wrote:


Apr 20, 2022, 1:47:28 PMApr 20
to Excel-DNA
Much appreciated, for helpful details and tips. Will keep in mind. 
Reply all
Reply to author
Message has been deleted
0 new messages