none
Office 2010 C# VSTO - how to programatically disable an add-in at shut-down? RRS feed

  • Question

  • Hi all,

    I'm currently writing an Excel 2010 add-in that needs to be disabled once Excel is closed.

    I tried the following code:

    private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
    {
            Office.COMAddIns adds = Application.COMAddIns;
            foreach (Office.COMAddIn addIn in adds)
            {
                if (addIn.ProgId == "MyCustomAddIn" && addIn.Connect)
                {
                    addIn.Connect = false;
                    break;
                }
            }
    }

    but it causes Excel to crash, and to the add-in to eventually not be disabled as I wish... Is there any other way to do this, other than changing the add-in's registry LoadBehavior entry to anything other than '3'?

    Tuesday, May 8, 2012 9:36 AM

Answers

  • what calling .Connect = false will do is that it will simply set that flag in registry, nothing else, so your quest for elegant solution boils down to calling function that will set registry and setting registry yourself. Not so much a worthy goal, imho.
    • Marked as answer by Shay.livyatan Tuesday, May 8, 2012 12:06 PM
    Tuesday, May 8, 2012 10:58 AM

All replies

  • remember that usually reg entries will be under HKLM and any changes there will require admin privilieges. You could bypass that by creating verbatim copy of your add-in subkey under HKCU with proper loadbehaviour. I do not think there is any other way to do it programmatically.
    Tuesday, May 8, 2012 10:22 AM
  • That's alright, I always install add-ins to current user so the key is always under HKCU - but still, I'd like to avoid messing around with the registry for such thing - there must be a more elegant solution for this...

    Maybe there is a way to tap into the Application_Shutdown event (or maybe even ThisAddIn_AfterShutdown?), and from there I could safely 'disconnect' the add-in without causing any instability to Excel?

    Tuesday, May 8, 2012 10:45 AM
  • what calling .Connect = false will do is that it will simply set that flag in registry, nothing else, so your quest for elegant solution boils down to calling function that will set registry and setting registry yourself. Not so much a worthy goal, imho.
    • Marked as answer by Shay.livyatan Tuesday, May 8, 2012 12:06 PM
    Tuesday, May 8, 2012 10:58 AM
  • Well yes, but it seems odd to me that calling .Connect = false makes Excel crash, and programatically setting LoadBehavior to '2' won't make it crash (I haven't tried it yet though)...

    I wonder why's that happening, because maybe I was just doing something wrong - and this is why I asked my initial question...

    Tuesday, May 8, 2012 11:06 AM
  • think about this - from inside your add-in AppDomain you are asking it to close your add-in - most probably excel tries to destroy your appdomain immediatly and you still hold reference to something that will try to run later, for example NativeWindow.
    Tuesday, May 8, 2012 11:59 AM
  • Yes, I know this is probably the problem - and again, this is why I was looking for another solution... Well, nevermind that now - I've decided to change the registry key on exit and it works fine, although I don't like this solution so much :)

    This is the code I used, in case someone would need it in the future:

    private void ThisAddIn_Shutdown(object sender, System.EventArgs e) {       RegistryKey registryKey = 

    Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Office\\Excel\\Addins\\MyCustomAddIn"true);       if (registryKey != null)       {            registryKey.SetValue("LoadBehavior", 2);       }

    }

    And thanks DamianD, I'll mark your relevant post as the answer (well, it was a solution so that's good enough :))...



    • Edited by Shay.livyatan Tuesday, May 8, 2012 12:12 PM argh... another code fix...
    Tuesday, May 8, 2012 12:05 PM
  • you are too kind good Sire ;-P
    Tuesday, May 8, 2012 12:27 PM
  • Indeed I am ;-)
    Tuesday, May 8, 2012 12:34 PM