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

31 views
Skip to first unread message

typedef

unread,
Apr 20, 2022, 11:35:28 AM4/20/22
to Excel-DNA
Hello 

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

Thanks,

Alex.

Mohummed Ibrahim

unread,
Apr 20, 2022, 11:54:15 AM4/20/22
to exce...@googlegroups.com
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 exceldna+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/exceldna/ef2e9b3f-77cd-4b16-a37d-d1ccc6fd2955n%40googlegroups.com.

Kedar Kulkarni

unread,
Apr 20, 2022, 12:32:45 PM4/20/22
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.

typedef

unread,
Apr 20, 2022, 1:46:25 PM4/20/22
to Excel-DNA
Thanks a lot, my friend.

On Wednesday, April 20, 2022 at 11:54:15 AM UTC-4 mohummed...@gmail.com wrote:

typedef

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