Excel-DNA / ExcelDna

Excel-DNA - Free and easy .NET for Excel. This repository contains the core Excel-DNA library.
https://excel-dna.net
zlib License
1.26k stars 270 forks source link

Multiple Dynamic COM Add-ins Loading #646

Closed NicholasHanafey closed 8 months ago

NicholasHanafey commented 8 months ago

I recently upgraded an existing add-in to a later version of ExcelDNA and, ever since the upgrade, multiple COM add-ins are loading dynamically in Excel:

image

The one with "Custom Task Pane Helper" was always there but I have no idea why the second one (the one highlighted in the image above) is now being created.

I have a feeling it has to do with a change I had to make to get a custom ribbon button to work with the new version of ExcelDNA. Previously I was adding a button to the AddIns tab via Application.CommandBars(1).Controls.Add but, since the upgrade, the button would not connect with the click event handler.

I have removed the old code and I am now using the element inside the .DNA file to create the UI and a new Ribbon class to house the event handler (a technique I found somewhere in the samples). I noticed that the Ribbon class has the ComVisible(True) decorator and thought maybe that was the cause, but if I remove the decorator my button does not show up AND the second mystery add-in still appears.

The add-in seems to be functioning fine but I would really like to understand what is going on here.

govert commented 8 months ago

Excel-DNA loads separate COM add-ins for the Ribbon extension and for the CTP Manager, both are required and this has not changed. So what you're seeing is the intended behaviour when you have both a ribbon and CTP. It's probably possible for an add-in to only expose and load a single add-in class that covers both aspects, but that's not the default or something I've tried.

That CommandBars approach to adding buttons does not require an extra COM add-in, so that's why you didn't have one before. I'm not sure why the event handler broke between versions - I do recall having heard something like this before but not the details.

I would suggest putting the ribbon markup xml into the GetCustomUI override of your ribbon class, instead of the .dna file, or into a resource file in your C# project. See https://github.com/Excel-DNA/Tutorials/tree/master/Fundamentals/RibbonBasics

I'm encouraging a move away from using the .dna files and putting properties into the project file directly, or into add-in code.

You should also take not of this issue, https://github.com/Excel-DNA/ExcelDna/issues/642 , which concerns problems with ribbon and CTP loading in the current Excel beta versions I don't yet know whether this will get to the release version, and it looks like we have a workaround in Excel-DNA 1.7.0-rc9.

NicholasHanafey commented 8 months ago

Thanks very much for the detailed response! That makes sense now. I have a backlog item to create the UI via GetCustomUI instead of the .DNA file, but thanks for the suggestion.