Matthew Murdoch
Matthew Murdoch

Reputation: 31453

How can I make use of .NET objects from within Excel VBA?

Can VBA code instantiate and use .NET objects? The specific class I'm interested in is System.IO.Compression.GZipStream.

For Info GAC is the .NET Global Assembly Cache

Upvotes: 9

Views: 15186

Answers (2)

Mike Rosenblum
Mike Rosenblum

Reputation: 12157

I think Andy nailed this answer, but I'm not certain that the aspect regarding the CLR loading rules is exactly right.

The .NET Assembly that holds the class acting as the wrapper for GZipStream would be exposed to COM and registered just like any other COM project library and class. In this regard, VBA would find the location of the COM-exposed .NET assembly via the registry. It might be smart to put the assembly in the GAC, so that it can't move (since moving the assembly would invalidate the registry info), but so long as the registry points to the right place, it should be fine.

A good beginner's tutorial on the subject is here

Hope this helps...

Upvotes: 12

Andy
Andy

Reputation: 30418

VBA can make use of any .NET objects that are exposed to COM. I don't know if GZipStream is or not, but I would guess that it would be easier for you to create a separate .NET object that is a wrapper around the functionality of GZipStream that you want to use. You can then expose your object to COM, and then VBA should make use of it.

Note that the assembly containing your COM object (and its type library too, I think, although I'm not positive on that) need to either be in the same directory as the main executable (winword.exe, or whatever) or in the GAC. This is due to the CLR's loading rules for assemblies.

Upvotes: 3

Related Questions