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

回眸只為那壹抹淺笑 提交于 2019-12-11 18:45:43

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/54245737/excel-worksheet-change-event-is-not-firing-when-two-workbooks-are-open

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!