c# – 活动工作簿更改时的事件

我正在写一个Excel AddIn.它使用功能区,其中某些控件启用/禁用,具体取决于工作簿的属性.

要做到这一点,我想我应该在活动工作簿更改时更新功能区控件的状态.

Chip Pearson的网站解释了how to do it in VBAhere解释了如何获取活动的Excel工作簿,但是我无法在我的C#AddIn中触发此事件.

最佳答案 将以下代码添加到ThisAddIn类中,事件似乎在需要时会升高.

    void Application_ActiveWorkbookChanges(Excel.Workbook Wb)
    {
        // TODO: Active Workbook has changed. Ribbon should be updated.    
    }

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        this.Application.WorkbookActivate   += 
            new Excel.AppEvents_WorkbookActivateEventHandler
                (Application_ActiveWorkbookChanges);
        this.Application.WorkbookDeactivate += 
            new Excel.AppEvents_WorkbookDeactivateEventHandler
                (Application_ActiveWorkbookChanges);
    }
点赞