Excel AddIn.Installed behavior

Add-in Express™ Support Service
That's what is more important than anything else

Excel AddIn.Installed behavior
 
Larry Sater




Posts: 16
Joined: 2016-10-26
My current project is a command-line tool that starts up Excel (via c# Excel COM Interop), loads a workbook and relies on my Excel add-in (QUERYTOOLXLL) that I created via Add-In Express to load in data.

I would appreciate some help on understanding why my Excel add-in automation works with the following code, i.e. why do I need this code? If I comment out the code it never hits my add-in. To make it work I have to set the Installed property to false and then set it back to true - just setting it to true doesn't load it either.

Any help would be appreciated, including a better way to do this.

Thanks.


xlApp = new Excel.Application();
foreach (Excel.AddIn item in xlApp.AddIns)
  {
    Console.WriteLine($"{item.Name}-{item.FullName}-IsInstalled={item.Installed}");
    if (item.FullName.ToUpper().Contains("QUERYTOOLXLL"))
    {
      item.Installed = false;
      item.Installed = true;
    }
}
Posted 22 Feb, 2017 12:49:35 Top
Andrei Smolin


Add-in Express team


Posts: 14137
Joined: 2006-05-11
Hello Larry,

An Excel add-in won't load if Excel is started programmatically/. This is by design; see https://support.microsoft.com/en-us/help/213489/add-ins-do-not-load-when-using-the-createobject-command-in-excel.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 23 Feb, 2017 06:27:07 Top
Larry Sater




Posts: 16
Joined: 2016-10-26
Thanks, Andrei,

This caused me to look a little further. The below article describes how to "install" the Excel add-in at runtime. Interestingly, they show the same switching of the ".Installed" flag that I had to do (I actually wouldn't doubt that I stole it from here and just forgot).

Loading Excel Add-In at Runtime

One more quick question if I may. I have been looking at your articles on the importance of releasing COM objects. In the code of my original post am I guilty of this? Should I change this to a For/Next loop and Marshal.ReleaseComObject on the .AddIn?

Thanks.
Posted 23 Feb, 2017 10:39:40 Top
Andrei Smolin


Add-in Express team


Posts: 14137
Joined: 2006-05-11
Hello Larry,

I recommend that you release COM objects whenever you can. This may prevent debug-time issues that would cost you far, far more than writing the corresponding code lines. We (you and me) don't know how Excel reacts to leaving these COM objects unreleased in *all* possible scenarios. Getting this knowledge in practice may cost you a lot. I would avoid such a risk.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 24 Feb, 2017 04:43:28 Top