在 VBA 中使用标准 .Net 库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4201127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using standard .Net libraries with VBA
提问by Evil Pigeon
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
按照此处发布的步骤从 Office 2003 执行 .NET 3.0 代码,我已经成功地运行了我自己的 .Net 代码
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.
有没有一种方法可以使用标准的 .Net 库而无需编写包装器?这样我们就可以避免在客户端机器上的 GAC 中注册和安装自定义 DLL。
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.
我已经在 C:\Windows\Microsof.NET\Framework 文件夹中找到了 tlb 文件,并且能够添加对 mscorlib.dll 的引用。查看RijndaelManaged的文档,这个类似乎是 COM 可见的。
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!
我愿意接受您提供的任何技巧!
回答by bob-the-destroyer
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.
还不可能。VBA(用于应用程序的 VB)不是 VB,而是一个单独的协议,它模仿了旧版本 VB 的所有基本语法。这几乎就像使用普通的旧 VB 的精简版本。事实上,VBScript 是最接近 VBA 的。也许有一天,Microsoft 会内置直接与 GAC 一起工作的方法,但在那之前(我敢肯定,那一天很可能意味着 COM 的死亡),您将被困在使用 COMCreateObject()
方法中,添加对已注册的 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 应用程序。
回答by Joe
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:
您应该能够以ComVisible
这种方式使用.NET 类。但是GenerateKey
和GenerateIV
方法没有返回值。尝试:
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