Add Events for Excel application

We can add the events for the office application
Following is the code for adding CellChange, BeforeBookClose events

Namespace part


using System;
using Excel;
using System.Reflection;


Code Part

public static AppEvents_WorkbookBeforeCloseEventHandler Event_BeforeBookClose;
public static DocEvents_ChangeEventHandler Event_ChangeEvent;

protected void Page_Load(object sender, EventArgs e)
{

Application MyExcelApplication = new ApplicationClass();
Workbook MyExcelWorkbook = MyExcelApplication.Workbooks.Add(Missing.Value);

MyExcelWorkbook.Windows.get_Item(1).Caption = "Using Delegates";

Worksheet MyExcelWorksheet1 = (Worksheet)MyExcelWorkbook.Worksheets.get_Item(1);
Worksheet MyExcelWorksheet2 = (Worksheet)MyExcelWorkbook.Worksheets.get_Item(2);
Worksheet MyExcelWorksheet3 = (Worksheet)MyExcelWorkbook.Worksheets.get_Item(3);

MyExcelWorksheet1.Activate();
Event_BeforeBookClose = new AppEvents_WorkbookBeforeCloseEventHandler(BeforeBookClose);
MyExcelApplication.WorkbookBeforeClose += Event_BeforeBookClose;

Event_ChangeEvent = new DocEvents_ChangeEventHandler(CellChange);
MyExcelWorksheet1.Change += Event_ChangeEvent;
MyExcelWorksheet2.Change += Event_ChangeEvent;
MyExcelWorksheet3.Change += Event_ChangeEvent;
MyExcelApplication.Visible = true;
MyExcelApplication.UserControl = true;
}
private static void CellChange(Range Target)
{

}
private static void BeforeBookClose(Workbook MyExcelWorkbook, ref bool Cancel)
{

}



Code Explanation

1. Create instance of the Application
2. Create instance of Workbook
3. Create instance of the Worksheet
4. Add Event Handler for the BeforeClose Event
5. Add Event Handler for the change Event
6. At the cell changes the event "CellChange" will be called
7. Before close the book the event "BeforeBookClose" will be called


By
Nathan


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: