简体   繁体   中英

Excel worksheet change event is not firing when two workbooks are open

We are creating a C# Excel Add-in and we want to trap the WorkSheet change event. We have the did the following:

private static DocEvents_ChangeEventHandler EventDel_CellsChange;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    this.Application.WorkbookOpen += Application_WorkbookOpen;
    EventDel_CellsChange = new DocEvents_ChangeEventHandler(WorksheetChangeEventHandler);
}

public void Application_WorkbookOpen(Workbook Doc)
{
    Sheets asp = Doc.Worksheets;
    foreach(Worksheet hoja in asp)
    {
        hoja.Change += EventDel_CellsChange;
    }
}

public void WorksheetChangeEventHandler(Range Target)
{
    MessageBox.Show("Cell has changed");
}

When we open the first workbook the code runs (message Cell has changed appears ). When we open the second one (without closing the first one) the message does not appear (WorksheetChangeEventHandler is not executed) in either workbook but the code in Application_WorkbookOpen always get execute though.

Any idea how to trap the WorkSheet change event when two workbooks are open?

You need to instantiate and deinstantiate each instance of a workbook and then work on the worksheet of each one. You also need to activate the workbook you are working on.

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