Custom ribbon and external library

Jul 12, 2011 at 1:04 PM
Edited Jul 12, 2011 at 1:15 PM

Hi,

I'm trying to create a custom ribbon using the customUI declaration in the .dna file.  I want it to call a method on a class that derives from ExcelRibbon.

Using the example, all works fine.  However, as soon as I move the code for the MyRibbon class into an external assembly, it fails to show my custom UI at all.  However, my UDFs continue to appear so it's obviously loading the xll correctly.

Is it possible to do this?  On the face of it, I can't see how excel-dna knows which class to wire the ribbon xml up to...

Thanks!

Miles

Coordinator
Jul 12, 2011 at 1:25 PM

Hi Miles,

Excel-DNA knows whether to hook up a class as a Ribbon by checking whether the class inherits from the ExcelRibbon class. But to work, the class must also be Com Visible.

So the mostly likely problem is that your ExcelRibbon-derived class is not Com Visible. By default (if no attributes are present anywhere), all types are Com Visible - that why it works when the code is in the .dna file. But when you make a new project with Visual Studio, the AssemblyInfo.cs gets an [assembly:ComVisible(false)] attribute, which makes all types not Com Visible by default. You can change this setting in the Assembly Information page for project - switch on 'Make assembly COM visible'. (Don't confuse this with 'Register for Com Interop' which is something totally different that you should keep off for Excel-DNA libraries.) You can also override this assembly-wide default on a per-type basis:

using System.Runtime.InteropServices;
using ExcelDna.Integration.CustomUI;

...

[ComVisible(true)]

public MyRibbon : ExcelRibbon 

{...}

It this is not it, please write back and we can dig a bit more.

-Govert

Jul 12, 2011 at 1:34 PM
Hi Govert,

Thanks for the quick answer - that was exactly it and it now works fine!

On the wiring up, I guess I was thinking if there were multiple ExcelRibbon-derived classes, how would it know which to use? But perhaps that's just wrong - I haven't produced a custom ribbon before so I don't know how you'd normally structure it.

Incidentally, a while back you helped me with a suggestion about using RTDs for async calls. I got it all working fine in the end (there are no special thread UI issues when using the RTD server) and I keep meaning to post an update but haven't found the time yet...

Thanks

Miles


On Tue, Jul 12, 2011 at 1:25 PM, govert <notifications@codeplex.com> wrote:

From: govert

Hi Miles,

Excel-DNA knows whether to hook up a class as a Ribbon by checking whether the class inherits from the ExcelRibbon class. But to work, the class must also be Com Visible.

So the mostly likely problem is that your ExcelRibbon-derived class is not Com Visible. By default (if no attributes are present anywhere), all types are Com Visible - that why it works when the code is in the .dna file. But when you make a new project with Visual Studio, the AssemblyInfo.cs gets an [assembly:ComVisible(false)] attribute, which makes all types not Com Visible by default. You can change this setting in the Assembly Information page for project - switch on 'Make assembly COM visible'. (Don't confuse this with 'Register for Com Interop' which is something totally different that you should keep off for Excel-DNA libraries.) You can also override this assembly-wide default on a per-type basis:

using System.Runtime.InteropServices;
using ExcelDna.Integration.CustomUI;

...

[ComVisible(true)]

public MyRibbon : ExcelRibbon 

{...}

It this is not it, please write back and we can dig a bit more.

-Govert

Read the full discussion online.

To add a post to this discussion, reply to this email (exceldna@discussions.codeplex.com)

To start a new discussion for this project, email exceldna@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com