Question

I'm converting a VB6 dll to VB.Net using Visual Studio 2008 Express. I want to use the same .dll to integrate with Excel via Excel-DNA, but also to be available via COM (I need to be able to call it from VBScript and VBA).

If I leave the assembly unsigned, I have access to all of the ExcelDNA functionality but no COM access.

If I sign the assembly with a strong name, then when I try to build the .dll I get the following error:

Unable to emit assembly: Referenced assembly 'ExcelDna.Integration' does not have a strong name

What are my options?

Was it helpful?

Solution

Excel-DNA has an option to expose your .NET classes to COM directly, so you can use them directly from VBA as regular COM classes.

To do this your class must be ComVisible (or the whole assembly must be ComVisible), and you must mark the ExternalLibrary as ComServer='true' in the .dna file, something like:

<DnaLibrary RuntimeVersion='v4.0' />
  <ExternalLibrary Path='MyAddIn.dll' ComServer='true' />
</DnaLibrary>

Then you have some options for registration: either call "Regsvr32.exe MyAddInDna.xll" or in the AutoOpen of your add-in, call ExcelDna.Integration.ComServer.RegisterServer()

Both of these set the registry entries so that the .xll file becomes the COM server for your classes. You can then access them late-bound from VBA, with CreateObject("MyNameSpace.MyClass").

You need another step to set up a type library (to give you intellisense in VBA). For this you generate the type library with tlbexp.exe. The regsvr32 / ComServer.RegisterServer call will find the type library and register it too.

You can also pack the .dll into your .xll file (adding a pack='true' attribute to the ExternalLibrary tag) and the type library will also be packed if present, and registered from the resource in the .xll file when you call Regsvr32.exe

So the end result could be a single file .xll add-in that is both an Excel Add-In with UDFs, ribbons, RTD servers and also a COM server that you can access from VBA.

More info in the discussions here: http://exceldna.codeplex.com/discussions/252721 and here: http://groups.google.com/group/exceldna/browse_frm/thread/4c5a71efbe96d885.

OTHER TIPS

You don't have to strong-name a [ComVisible] assembly. It is only required when you want to install it in the GAC. Not strictly necessary although not a bad idea to fight DLL Hell. You need to register it with Regasm.exe using the /codebase option. Visual Studio already does that automatically, although the option might be missing in the Express edition.

Fixing the second problem shouldn't be hard either. Just rebuild the Excel-DNA solution from the source code you can download from Codeplex.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top