On a daily basis, a person needs to check that specific workbooks have been correctly updated with Bloomberg and Reuters market data; i.e. all data has pulled through and that the 'numbers look correct'. In the past, people were not checking the 'numbers' which led to inaccurate uploads to other systems.
The idea is that 'something' needs to be developed to prevent the use from closing/saving the workbook unless he/she has checked that the updates are correct/accurate. The numbers look correct
action is purely an intuitive exercise, thus will not be coded in any way.
The simple solution was to prompt users prior to closing the specific workbook to verify that the data has been checked.
Using VSTO SE for Excel 2007, an Add-in was created which hooks into the WorkbookBeforeClose
event which is initialised in the add-in ThisAddIn_Startup
private void wb_BeforeClose(Xl.Workbook wb, ref bool cancel)
{
//.... snip ...
if (list.Contains(wb.Name))
{
DailogResult result = MessageBox.Show("some message", "sometitle", MessageBoxButtons.YesNo);
if (result != DialogResult.Yes)
{
cancel = true; // i think this prevents the whole application from closing
}
}
}
I have found the following ThisApplication.WorkbookBeforeSave
vs ThisWorkbook.Application.WorkbookBeforeSave
which recommends that one should use the ThisApplication.WorkbookBeforeClose
event which I think is what I am doing since will span all files opened.
The issue I have with the approach is that assuming that I have several files open, some of which are in my list
, the event prevents Excel from closing all files sequentially. It now requires each file to be closed individually. Edit: this happens when Exit Excel is used from File menu.
Questions
- Am I using the
WorkbookBeforeClose
event correctly and is this effective & efficient use of the event? - Should I use the Application level event
or document level event? - Is the behaviour described above normal?
- Any other suggestions are welcomed when using workbook events in an add-in
Update [30-Mar-2010]:
Tinkering around, I also tried the following which attempted to bind the BeforeClose
event handler to every workbook that was opened as suggested from the link above.
private void ThisAddIn_Startup(...)
{
// snip
Globals.ThisAddin.Application.WorkbookOpen += Application_Open;
}
private void Application_Open(XL.Workbook wb)
{
wb.BeforeClose += Document_WorkbookBeforeClose; // method does the same as above
}
The problem that I found with this approach is that is I try to close to all Excel Files (using the Exit Excel option) the event handler does not execute. From my observation, this happens when the document to be checked is not the active document.
This method seems erratic when compared to my initial approach. The one thing I am not certain about or feel comfortable with is binding the event every time a document is opened.
Update [07-Apr-2010]:
Glen's suggested answer is useful but does not tackle the immediate questions at hand, I have thus clarified the last question a bit further.
I have also found this blog How to Get an Excel VSTO Workbook Closed Event which is somewhat relevant to my issue as it could be used within an alternative approach to my solution using a monitor-type approach to handling the workbooks (and possibly also use the newly introduced OnWorkbookClosed
event).
Update [08-Apr-2010]:
There seems to be some confusion, I am not concerned about any validation on the workbooks themselves but rather whether the method I am using (i.e. using the Application-level WorkbookBeforeClose
event) is correct.
@Mathias' comment below shows the correct understanding of part of the problem in relation to question 3, I think that this is default excel behaviour though. The solution to overcome this was to create a close function that closes only my specific files.
- Is the behaviour described above normal? Yes, but why? Because the add-in hooks into the application-level event, the checks and cancellation of the event blocks the application from closing any further workbooks. The key here is the
ref bool cancel
argument (cancel=false
allows normal closing of the workbook(default),cancel=true
prevents the workbook from closing)
VS 2005 with VSTO SE