Main menu

How to: Programmatically Re-enable an Add-in That Has Been Disabled (VSTO)

Microsoft Office applications can disable add-ins that behave unexpectedly. If an application does not load your add-in when you try to debug it, the application might have hard disabled or soft disabled your add-in.- MSDN.

To enable the Add-in back via the office application you have to follow whats mentioned in this link.

Programmatically Enabling Soft Disabled Add-ins

Soft disabling can occur when an add-in produces an error that does not cause the application to unexpectedly close. For example, an application might soft disable an add-in if it throws an unhandled exception while the Startup event handler is executing.

Now lets see how we can enable this programmatically. for the sample I’m using PowerPoint.

 

            PowerPoint._Application app = null;
            app = Marshal.GetActiveObject("PowerPoint.Application") as PowerPoint._Application;
 
            object index = "your app";
 
            COMAddIn addin = null;
            addin = app.COMAddIns.Item(ref index);
            if (addin.Connect)
            {
 
            }
            else
            {
                addin.Connect = true;
            }

Programmatically Enabling Hard Disabled Add-ins

Hard disabling can occur when an add-in causes the application to close unexpectedly. It might also occur on your development computer if you stop the debugger while the Startup event handler in your add-in is executing.

To enable add-ins that are Hard disabled you might have to play a little with registry. specifically the registry entry at :

“HKEY_CURRENT_USER\Software\Microsoft\Office\15.0\PowerPoint\Resiliency\DisabledItems”

Basically your add-in should have an entry here. and its value is in binary format. if you delete the entry it might enable you add-in again. but blindly deleting all entries might enable any other add-ins too. mostly this binary value will contain the application path (ClickOnce Url or the installation path) encoded. to specifically delete your add-in you might have to do the following :

 

    var key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Office\15.0\PowerPoint\Resiliency\DisabledItems", true);
            var names = key.GetValueNames();
            foreach (var item in names)
            {
                var valueKind = key.GetValueKind(item);
 
                if (valueKind == RegistryValueKind.Binary)
                {
                    var regValue = (byte[])key.GetValue(item);
                    BinaryReader binaryReader = new BinaryReader(new MemoryStream(regValue));
                    binaryReader.ReadInt32();
                    int pathLength = binaryReader.ReadInt32(); // The next four bytes are the length of the path
                    binaryReader.Close();
 
 
                    if (regValue.Length >= 12 + pathLength)
                    {
                        string path = Encoding.Unicode.GetString(regValue, 12, pathLength - 2);
                        if (path.ToLower().Contains("your application path"))// something to uniquely identify your application
                        {
 
                            key.DeleteValue(item);                          
                        }
                    }
                }
            }

(Note: office application version and application name might be different based on your office add-in).

For detailed information on  VSTO Add-in loading issues please refer this link.

To turn off document recovery refer this post.

.Hope it helps :).

Aneef Fashir (41 Posts)

Software Architect @ Assette, Sri Lanka


Tags

Archives