Search code examples
excelvstoribbon

Excel VSTO access sheet name through ThisAddIn StartUp


I am stuck and hoping to get some help here. I have an Excel VSTO add-in with a ribbon that contains various controls. I would like to have all but one control disabled unless a sheet with a specific name is active. How is this accomplished? So far, I figured out how to map the ribbon button to the ThisAddIn startup in order to keep it active, while the other buttons default to disabled:

RibbonButton btx_newECO = Globals.Ribbons.Ribbon1.btn_ECOnumber;
btx_newECO.Enabled = true;

Now, how do I enable the other ribbon buttons when the sheet with a specific name is opened? `


Solution

  • Okay, Cindy got me going in the right direction. Rather than using a SheetActivateHandler I used a WorkBookOpenHandler. I had no idea I could assign event handlers this way. I totally learned this today. Thanks, Cindy :)

    --ThisAddIn---

    public partial class ThisAddIn
    {
        Ribbon1 rbn;
        string triggerName = "ECO";
    
        #region StartUp
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            //detect the sheet name on workbook open event
            rbn = Globals.Ribbons.Ribbon1;
            this.Application.WorkbookOpen+= new Excel.AppEvents_WorkbookOpenEventHandler(Application_WorkbookOpen);
        }
    
        #endregion
    
        #region Enable Ribbon Buttons
        void Application_WorkbookOpen(object Sh)
        {
            Excel.Worksheet ws = (Excel.Worksheet)Globals.ThisAddIn.Application.ActiveSheet;
            if (ws.Name == triggerName)
            {
                rbn.btn_ECOnumber.Enabled = false;
            } 
        }
        #endregion
    

    ---Shutdown and VSTO generated Code here-----