简体   繁体   中英

Which event should I fire in C# for programming a newly created excel file?

If we want to fire an event when a user hits the save button, we use Globals.ThisAddIn.Application.WorkbookAfterSave += Application_WorkbookAfterSave;

Similarly, when we want to fire an event when a user opens a saved file, we use Globals.ThisAddIn.Application.WorkbookOpen += Application_WorkbookOpen;

My question is, which event should we use when a user creates a new file and it is not saved yet. If there's no such event, please tell me anyway of achieving my task of doing some programming as soon as user creates a new excel file.

Looks like you are interested in the NewWorkbook event of the Application class in Excel.

  ((Excel.AppEvents_Event)Application).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 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