views:

225

answers:

2

Hello All, I am trying to gain access to a .NET class library in Microsoft Excel. To do this I know that the .NET class library must be registered with COM. So I tried going to my Assembly Info and Setting COM Visible to true. Then on the build tab I set Register for COM Interop for true also. I checked the AssemblyInfo.cs file and it does contain [assembly: ComVisible(true)]. But for some reason when I try to add a reference to the Class Lib in Excel the namespace does not show up in the list. I made a quick test Class library with nothing in it and did the same thing (set COM Vis = true , and Register For COM Interop = true) and that one does show up on the list of available references. I can't figure out what the difference is between the two classes. I am not sure if my class is actually being registered for COM interop or not. Does anyone know what I can do to fix this???

Added: I have tried to register the class manually using RegAsm but I get the following message... "RegAsm : warning RA0000 : No types were registered"

Added: I check my project output directory and I noticed that a type library file (*.tlb) is not being created when I build the project. I made sure that each class has it's own GUID attribute. The project does not contain any structs, interfaces, or enum's.

A: 

Check the security for addins in Excel. Also make sure you log everything to a file, MS Office tends to eat any exceptions raised while running the addin.

eschneider
But I can't even find the class in the list to add a reference to it.
Jordan S
Did you use the Browse button and then try and find your dll?
eschneider
Yes I tried that and when I click it it says that I can not load the specified reference.
Jordan S
Select the *.tlb file instead
eschneider
the .tlb file is not being generated when I build the project. This is the root of my problem.
Jordan S
Try to sign the assembly (project settings).
eschneider
Ok, I tried signing it and it still doesn't work.
Jordan S
Make sure you start Visual Studio as "Run as Admin" (right click icon), otherwise register com fails and tlb file is not created during build.
eschneider
I was running as administrator
Jordan S
I don't mean are you a admin, I mean did you start VS with Run as Admin, it starts as non-admin regardless...
eschneider
Yeah, ever since Vista I have had Visual Studio set to always run as administrator.
Jordan S
+1  A: 

Alright I found the problem, which was actually caused by two problems. When I would build the project in Visual Studio I noticed that the type library file was not being created. I discovered a warning that told me no classes were found that could be registered so registration was not occurring. As I dug a little deeper I found these two rules. 1. Static classes will not register with COM. 2. All classes must have a public default constructor to be instantiated through COM

From MSDN: Types must have a public default constructor to be instantiated through COM. Managed, public types are visible to COM. However, without a public default constructor (a constructor without arguments), COM clients cannot create an instance of the type. COM clients can still use the type if the type is instantiated in another way and the instance is returned to the COM client. You may include overloaded constructors that accept varying arguments for these types. However, constructors that accept arguments may only be called from managed (.NET) code.

Jordan S