4

I'm writing an Excel AddIn. It uses a Ribbon where some Controls are enabled/disabled depending on the properties of the Workbook.

To do it I guess I should update the state of the Ribbon controls whenever the active Workbook changes.

Chip Pearson's site explains how to do it in VBA and here is explained how to get the active Excel Workbook, however I'm not able to trigger this event in my C# AddIn.

Community
  • 1
  • 1
Jaime Oro
  • 9,899
  • 8
  • 31
  • 39

1 Answers1

2

Adding following code to ThisAddIn class the event seems to raise up when required.

    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);
    }
Jaime Oro
  • 9,899
  • 8
  • 31
  • 39
  • Solution is based on the [page](http://msdn.microsoft.com/en-us/library/office/aa168477(v=office.11).aspx) proposed by @TimWilliams and [this early question](http://stackoverflow.com/questions/7180318/c-sharp-using-the-event-handler-workbookopen). – Jaime Oro Aug 07 '12 at 19:48
  • ¿Is this solution enought effective? ¿Is there any potential problem? – Jaime Oro Aug 07 '12 at 19:48