从 C# 引用 VBA 程序集的运行时错误

Runtime error referencing VBA assembly from C#(从 C# 引用 VBA 程序集的运行时错误)
本文介绍了从 C# 引用 VBA 程序集的运行时错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 C# .NET 3.5 项目,不幸的是它依赖于几个 VB6 ActiveX 控件.

I have a C# .NET 3.5 project that unfortunately relies on a couple of VB6 ActiveX controls.

到目前为止,我已经能够愉快地访问 ActiveX 控件中定义的数据容器和方法,只需在我的项目中添加对控件的引用并正常引用它们.

Up until now I have been able to happily access the data containers and methods defined in the ActiveX controls by simply adding a reference to the controls in my project and referencing them as normal.

但是,当我不得不调用返回 VBA.Collection 的方法时,我今天遇到了问题.

However I hit a snag today when I had to call a method that returns a VBA.Collection.

我的代码可以正常编译,但在运行时出现以下错误:

My code will compile with no problem, but at runtime I get the following error:

无法加载文件或程序集Interop.VBA,版本=6.0.0.0,Culture=neutral,PublicKeyToken=null"或其依赖项之一.系统找不到指定的文件.

Could not load file or assembly 'Interop.VBA, Version=6.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

我的项目中有对 Interop.VBA(6.0.0.0 版)和 Interop.VBRUN(6.0.0.0 版)的引用,有什么想法吗?

I have references to Interop.VBA (version 6.0.0.0) and Interop.VBRUN (version 6.0.0.0) in my project, any thoughts?

我无法提供具体代码,它包含在 NDA 中.

I can't give specific code, it is covered under an NDA.

但是,这里是来自对象浏览器的示例代码,更改了名称以保护不那么无辜;)

However, here is sample code from object browser with names changed to protect the not so innocent ;)

我可以从我的代码中引用以下属性和方法:

I can reference the following properties and methods from my code:

ReturnConstants GoodMethod(); 
string prop1 
ReturnConstants prop2 

如果我尝试可以调用此方法,我会在运行时收到上述错误消息:

If I try can call this method I get the error message above at runtime:

Collection BadMethod();

所以它调用返回导致问题的 VBA.Collection 的方法.这些是我无法控制的第 3 方控件,它们不会在 .net 中重写

So it the call to the method that returns a VBA.Collection that causes the issue. These are 3rd party controls that I have no control over and they will not be rewritten in .net

出于兴趣,我在 VS2010 中尝试了一个快速的虚拟项目,并按预期完美运行,因此看来这可能是 VS2012 的问题.我看到一些帖子暗示 2012 年没有为 VBA 正确创建互操作,所以我去看看我能找到什么......

edit: Out of interest I went and tried a quick dummy project in VS2010 and worked perfectly as expected, so it appears that this may be an issue with VS2012. I've seen some posts that hint at 2012 not creating interops for VBA correctly, so I'll go see what I can find there ...

推荐答案

感谢@Jeremy Thompson 让我走上正轨,运行进程监视器显示我的应用程序无法找到 interop.VBA.dll 文件,一旦修复,一切都会按预期工作.不知道为什么这在 2010 年而不是 2012 年开箱即用,但很高兴有一个解决方案

Thanks to @Jeremy Thompson for putting me on the right track, running process monitor showed me that my application was unable to locate the interop.VBA.dll file, once that was fixed everything works as expected. No idea why this works out of the box on 2010 and not 2012, but happy to have a solution

这篇关于从 C# 引用 VBA 程序集的运行时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)