Can an add-in be activated by a spreadsheet file? RRS feed

  • Question

  • I would like to write a C# add-in which hooks the application.SheetSelectionChange event and places a button on the row if that row contains order detail information.  But this add-in should only be active and hooking that event for certain spreadsheet files - those that contain order detail information.

    Is there a way Excel can activate and load my add-in based on some sort of trigger in the excel worksheet itself?

    Wednesday, February 25, 2015 1:12 PM


  • Hi Steve,

    Excel add-in don't have the ability to load or unload itself. Actually you could always keep the add-in loaded for all the workbooks, and use Workbook.Name to determine if this is the one you want to do some operations for. If it's not, just ignore it. For example:

    Excel.Workbook workbook = Globals.ThisAddIn.Application.Workbooks[1];
                if (workbook.FullName == "xxx.xlsx")
                    //do something...
                    workbook.SheetSelectionChange += new Excel.WorkbookEvents_SheetSelectionChangeEventHandler(workbook_SheetSelectionChange);
                    workbook.SheetSelectionChange -= new Excel.WorkbookEvents_SheetSelectionChangeEventHandler(workbook_SheetSelectionChange);

    If you want to load it for only some specified workbooks and not to load it for others, you need some other ways. One method is to create another add-in in which you will need to check the workbook name, then load or unload the other add-ins:

    foreach (COMAddIn addin in Globals.ThisAddin.Application.COMAddins)
        addin.Connect = true;                

    Another method is to utilize the VBA code to load or unload the add-in:

    AddIns("Add-In Name").Installed = True

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Steve Richter Friday, February 27, 2015 5:58 PM
    Friday, February 27, 2015 2:44 AM