简体   繁体   中英

Excel VSTO WorkbookNew event

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM