3
votes

In one Excel Add-in project for Excel 2007 I need to check the event for the creation of a new workbook. I also needed to catch the Workbook_Open event which I did quite easily... On my research on the internet I found the following:

Application.WorkbookOpen is raised when any workbook is opened. Excel passes the Workbook that is opened as a parameter to this event. This event is not raised when a new blank workbook is created. The Application.WorkbookNew event is raised instead.

Unfortunately, I am unable so far to find the Application.WorkbookNew event... I am missing something?

After typing Application. the autocomplete provides a nice and long list of events for the Workbook (Open included), but I can't find the WorkbookNew event...

Any ideas?

Thank you!

1

1 Answers

7
votes

You're looking for the Application.NewWorkbook event. Here's the VBA reference. And here is a an example in C#/VB.Net

Edit:

I cannot confirm the validity of this information, but I found the following explanation(link):

The NewWorkbook event is an application level event. Since there's also a property of the same name, Intellisense won't show this to you unless you explicitly cast the application object to the application events:

        ((Excel.AppEvents_Event)ThisApplication).NewWorkbook += new Microsoft.Office.Interop.Excel.AppEvents_NewWorkbookEventHandler(ThisWorkbook_NewWorkbook);

The event handler:

    void ThisWorkbook_NewWorkbook(Microsoft.Office.Interop.Excel.Workbook Wb)
    {
        MessageBox.Show("New workbook" + Wb.Name);
    }

The procedure you were trying to work with is only valid in the ThisWorkbook VBA project. It can't be used outside the immediate Excel environment.