Click here to Skip to main content
15,886,080 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have an application that uses VBA to interface with libraries written in .NET. The assemblies for these libraries is then registered as COM components. I am trying to get rid of the COM registration part. Essentially, I want the application to be able to function as long as the assembly is present, regardless of whether or not the assembly has been registered.

From what I can gather, it seems to be possible. I found some promising looking code samples here and here. From these, I have tried the following:
VB
Application.VBE.ActiveVBProject.References.AddFromFile "C:\SomeDirectory\SomeAssembly.dll"

and
VB
Dim VBAEditor As VBIDE.VBE
Dim VBProj As VBIDE.VBProject

Set VBAEditor = Application.VBE
Set VBProj = VBAEditor.ActiveVBProject

VBProj.References.AddFromFile "C:\SomeDirectory\SomeAssembly.dll"


Neither works. The first one tells me "Expecting a var name", while the second one complains that "Type identifier is invalid".


So, anyone know what I'm doing wrong?


Also, I'm VERY new to VBA; so, if possible, please keep the explanations simple.
Posted
Comments
Sergey Alexandrovich Kryukov 17-Jan-12 3:21am    
Is there any particular reason for using VBA at all? Why not using VB.NET?
--SA
BotCar 17-Jan-12 3:37am    
Don't ask me, I'm just the coding monkey that gets to fix stuff ;-)

To be honest, I'm not sure. The application was created by an external vendor before I joined the team I'm in now, so I know very little about the reasoning behind it. However, would VB.NET be able to run within a host application like VBA can?

1 solution

There are problems.... mostly related to security... when you try to mix & match VBA and .net code. The COM wrapper marshals all of this for you which is why it will work if it is registered. Also... I think that the AddFromFile method is looking for the RegisterCOMComponent entry point which doesn't exist in a straight .net assembly but does if the assembly has been marked for COM interop. (Try running regsvr32 on a regular .net assembly. You should get a message about the entry point missing)

You don't talk about what the host app is so it's hard to say if a pure .net solution would be available. If it is MS Office 2007 or higher, you can do everything (and more) in .NET using the Office components. Plus, it will fit into the security scheme. For example, if you code-sign the add-in, users will not get prompted for security warnings when trying to run the add-in.

A pure .net solution would also let you define interfaces and discover/load assemblies at runtime. Handy for swapping out some functional component while leaving the rest of the app the same.
 
Share this answer
 
Comments
BotCar 18-Jan-12 3:00am    
Hmm, looks like I'll just have to be satisfied with registering the assembly. Thanks for the info.

BTW, I didn't mention what the host app is because I didn't (and still don't) think that it's important. Rest assured that it is not Office, or anything similarly common (If it was, I would've mentioned it).

I also would've preferred a pure .NET approach, but unfortunately that does not seem to be an option here.
Jason Gleim 18-Jan-12 9:03am    
If you wanted to avoid registration because you needed to swap out different versions of the libraries and let the target app 'find' the latest version by loading it locally, you could write a helper library that did that for you. It would expose your classes as interfaces that VBA could work with but contain logic to locate and instantiate the non-registered dll(s) at runtime. The helper library would, of course, have to be registered so VBA could bind against it but by doing this you can uncouple the VBA and the .net. A lot more complicated but very doable.
BotCar 18-Jan-12 9:11am    
That's quite a nice idea. Thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900