Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using standard .Net libraries with VBA

Tags:

.net

vba

I have successfully been able to run my own .Net code by following the steps posted here Execute .NET 3.0 code from Office 2003

Is there a way to use the standard .Net libraries without having to write a wrapper? This way we can avoid having to register and install a custom DLL into the GAC on the client's machine.

I've found tlb files already in the C:\Windows\Microsof.NET\Framework folders, and have been able to add a reference to mscorlib.dll. Looking at the documentation for RijndaelManaged, this class appears to be COM visible.

I am able to create an instance, but as soon as I try and work with it, I get errors (e.g. "Type mismatch").

Sub Macro1()
   Dim aesImplementation As New RijndaelManaged

   Set key = aesImplementation.GenerateKey()
   Set iv = aesImplementation.GenerateIV()
End Sub

I am willing to accept any hacks you have to offer!

like image 646
Evil Pigeon Avatar asked Nov 17 '10 03:11

Evil Pigeon


People also ask

Does VBA use .NET framework?

Microsoft Visual Basic for Applications (VBA) uses unmanaged code that is tightly integrated with Office applications. Microsoft Office projects created by using Visual Studio enable you to take advantage of the . NET Framework and Visual Studio design tools.

How do I add a library to VBA?

In Microsoft Excel, you can add a library reference to the VBA project by clicking the Tools > References… manually. It will open the following dialog box which will help you to select from existing references or browse your library yourself.

Can you use C# in VBA?

It is possible for VBA code (which is a COM component) to call C# code. VBA can call . NET managed code by using a COM Callable Wrapper (CCW). To allow your C# code to be accessible from VBA you need to make sure that all the necessary types are COM visible.


2 Answers

You should be able to use ComVisible .NET classes in this way. However the GenerateKey and GenerateIV methods don't have a return value. Try:

Sub Macro1()
   Dim aesImplementation As New RijndaelManaged

   aesImplementation.GenerateKey
   Key = aesImplementation.Key
   aesImplementation.GenerateIV
   IV = aesImplementation.IV
End Sub

or better, not least because when debugging you can see whether an error occurs during construction or when you call the method:

Sub Macro1()
   Dim aesImplementation As RijndaelManaged
   Set aesImplementation = New RijndaelManaged

   aesImplementation.GenerateKey
   Key = aesImplementation.Key
   aesImplementation.GenerateIV
   IV = aesImplementation.IV
End Sub
like image 31
Joe Avatar answered Sep 23 '22 03:09

Joe


Not yet possible. VBA (VB for Applications) is not VB, but rather a separate deal mimicking all the basic syntax as older versions of VB. It's almost like using a very stripped down version of regular, older VB. In fact, VBScript is the closest match to VBA. Perhaps someday, Microsoft will build in methods to work directly with the GAC, but until then (and that day will likely mean the death of COM I'm sure), you're stuck using COM CreateObject() method, adding a reference to a COM registered library to your Office project, or directly referencing a VBA/COM compatible DLL or TLB file in your Office project.

There are quite a few COM-enabled libraries in the default GAC, but for the majority, you are stuck creating a Com Callable Wrapper first in VB.Net or C#.

Conversely, pretty much all MS Office apps are COM callable, so you can work with installed Office apps through VB.Net projects all you want.

like image 63
bob-the-destroyer Avatar answered Sep 21 '22 03:09

bob-the-destroyer