简体   繁体   English

在VBA中使用标准.Net库

[英]Using standard .Net libraries with 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 我已经成功地运行了我自己的.Net代码,遵循此处发布的步骤从Office 2003执行.NET 3.0代码

Is there a way to use the standard .Net libraries without having to write a wrapper? 有没有办法使用标准的.Net库而无需编写包装器? This way we can avoid having to register and install a custom DLL into the GAC on the client's machine. 这样我们就可以避免在客户端计算机上注册并安装自定义DLL到GAC中。

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. 我发现tlb文件已经存在于C:\\ Windows \\ Microsof.NET \\ Framework文件夹中,并且能够添加对mscorlib.dll的引用。 Looking at the documentation for RijndaelManaged , this class appears to be COM visible. 查看RijndaelManaged的文档,此类似乎是COM可见。

I am able to create an instance, but as soon as I try and work with it, I get errors (eg "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! 我愿意接受你提供的任何黑客行为!

You should be able to use ComVisible .NET classes in this way. 您应该能够以这种方式使用ComVisible .NET类。 However the GenerateKey and GenerateIV methods don't have a return value. 但是, GenerateKeyGenerateIV方法没有返回值。 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

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. VBA(VB for Applications)不是VB,而是一个独特的协议,模仿所有基本语法作为旧版本的VB。 It's almost like using a very stripped down version of regular, older VB. 这几乎就像使用普通的旧版VB的精简版。 In fact, VBScript is the closest match to VBA. 事实上,VBScript是最接近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. 也许有一天,微软将建立直接与GAC合作的方法,但在那之前(那天可能意味着COM的死我确定),你被困在使用COM CreateObject()方法,添加一个引用COM注册库到您的Office项目,或直接引用Office项目中的VBA / COM兼容DLL或TLB文件。

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#. 默认GAC中有相当多的支持COM的库,但对于大多数人来说,首先要在VB.Net或C#中创建一个Com Callable Wrapper。

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. 相反,几乎所有MS Office应用程序都是COM可调用的,因此您可以通过VB.Net项目使用所需的所有Office应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM