Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 719909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:41:53+00:00 2026-05-14T05:41:53+00:00

On a daily basis, a person needs to check that specific workbooks have been

  • 0

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

  1. Am I using the WorkbookBeforeClose event correctly and is this effective & efficient use of the event?
  2. Should I use the Application level event or document level event?
  3. Is the behaviour described above normal?
  4. 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.

  1. 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T05:41:53+00:00Added an answer on May 14, 2026 at 5:41 am

    The application-level WorkbookBeforeClose is the one to use. The issue is that erratic behaviour is resulting from files not being saved when WorkbookBeforeClose is called. If they are not, the Save event will kick and you’ve effectively now lost the WorkbookBeforeClose event because it has already occured. Here’s some VBA code that helps handle this.

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
        If Not Me.Saved Then
            NotSavedPrompt = Me.Name & " has not been saved. Would you like to save now?"
            SaveYesNo = MsgBox(NotSavedPrompt, vbQuestion + vbYesNoCancel)
            Select Case SaveYesNo
                Case vbYes
                    Me.Save
                Case vbNo
                    Me.Saved = True
                Case vbCancel
                    Cancel = True
                    Exit Sub
              End Select
        End If
        Call MyRoutine() //'this should be your sub that does what you want
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some python scripts that run on a daily basis in cron. How
I have a sql database that is written to on a daily basis by
We have with success been updating our development web site at a daily basis
We have a staging table (DB2) that receives production numbers on a daily basis
I have a forum where people on a daily basis post links to other
I've been getting more and more comfortable using vi on a daily basis, but
I have a daily cron job that email PDFs to customers. I want to
I have a database full of time-sensitive data, so on a daily basis I
I want Google Maps markers that will: Change on a daily basis Be loaded
I have a DOS batch file to run on a daily basis. Something similar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.