Call "Workbook_BeforeClose" event

Jul 31, 2012 at 2:35 PM


I'm developing a "public class MyRibbon : ExcelRibbon" , and i'm trying to call a "Workbook_BeforeClose" event, 

I tried OnBeginShutdown(ref Array custom) and I succeeded, probably because it just do "override".

What I am trying to do before the workbook is closed, calling a method that displays a message box.


Something like:


private override void Workbook_BeforeClose(bool cancel)


Jul 31, 2012 at 3:02 PM

Hi Diogo,

The Excel Application has a WorkbookBeforeClose event.

So in your class you need a variable of type Application (with a reference to the Microsoft.Office.Interop.Excel interop assembly), and then register an event handler for it.


Jul 31, 2012 at 3:39 PM
Edited Jul 31, 2012 at 3:40 PM

Hi Govert

I've tried that:

private void WorkbookBeforeClose(){app.ActiveWorkbook.BeforeClose +=new Excel.WorkbookEvents_BeforeCloseEventHandler(ThisWorkbook_BeforeClose);}

void ThisWorkbook_BeforeClose(ref bool Cancel) {MessageBox.Show("bye");}


but the Event is never called.

what do you mean by "register an event handler" ?


Jul 31, 2012 at 4:03 PM
Edited Jul 31, 2012 at 4:04 PM

Perhaps put the code you have in WorkbookBeforeClose into an override of ExcelRibbon.OnConnection?

But what if your add-in loads before the workbook is open?

You can also store the Application object you get in the ExcelRibbon.OnConnection, and then register for the Application object's WorkbookBeforeClose event, instead of the Workbook object's BeforeClose event.