This project has moved. For the latest updates, please go here.
/*
   using events in Excel
   taken from Example08.cs
*/
public void Example08Main()
{ 
   // start excel and turn off msg boxes
   Excel.Application excelApplication = new Excel.Application();
   excelApplication.DisplayAlerts = false;
   excelApplication.Visible = true;

   /*
   we register some events. note: the event trigger was called from excel, 
means an other Thread. remove the Quit() call below and check out
more events if you want. you can get event notifys from various objects:
Application or Workbook or Worksheet for example */
excelApplication.NewWorkbookEvent +=
new Application_NewWorkbookEventHandler(Application_NewWorkbook);
excelApplication.WorkbookBeforeCloseEvent +=
new Application_WorkbookBeforeCloseEventHandler(Application_WorkbookBeforeClose);
excelApplication.WorkbookActivateEvent +=
new Application_WorkbookActivateEventHandler(Application_WorkbookActivate);
excelApplication.WorkbookDeactivateEvent +=
new Application_WorkbookDeactivateEventHandler(Application_WorkbookDeactivate);
excelApplication.SheetActivateEvent +=
new Application_SheetActivateEventHandler(Application_SheetActivateEvent);
excelApplication.SheetDeactivateEvent +=
new Application_SheetDeactivateEventHandler(Application_SheetDeactivateEvent); // add a new workbook add a sheet and close Excel.Workbook workBook = excelApplication.Workbooks.Add(); workBook.Worksheets.Add(); workBook.Close(); excelApplication.Quit(); excelApplication.Dispose(); } void Application_SheetDeactivateEvent(COMObject Sh) { Console.WriteLine("Event SheetDeactivate called"); Sh.Dispose(); } void Application_SheetActivateEvent(COMObject Sh) { Console.WriteLine("Event SheetActivate called"); Sh.Dispose(); } void Application_NewWorkbook(Excel.Workbook Wb) { Console.WriteLine("Event NewWorkbook called"); Wb.Dispose(); } void Application_WorkbookBeforeClose(Excel.Workbook Wb, ref bool Cancel) { Console.WriteLine("Event WorkbookBeforeClose called"); Wb.Dispose(); } void Application_WorkbookActivate(Excel.Workbook Wb) { Console.WriteLine("Event WorkbookActivate called"); Wb.Dispose(); } void Application_WorkbookDeactivate(Excel.Workbook Wb) { Console.WriteLine("Event WorkbookDeactivate called"); Wb.Dispose(); }

Last edited May 14, 2012 at 4:54 AM by SebastianDotNet, version 8

Comments

No comments yet.