无法从 VBA (VB6 ok) 实例化用 C# 编写的 COM 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/375457/
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
Can't instantiate a COM object written in C# from VBA (VB6 ok)
提问by
Using VS 2008, here is my COM object
使用 VS 2008,这是我的 COM 对象
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace TestCom
{
[Guid("9E5E5FB2-219D-4ee7-AB27-E4DBED8E123E")]
[ClassInterface(ClassInterfaceType.AutoDual)]
[ProgId("Test9.COMINT")]
public class TestComClass
{
public void Init(string userid, string password)
{
MessageBox.Show(string.Format("{0}/{1}", userid, password));
}
}
}
If I build this and register it on a production machine as follows
如果我构建它并在生产机器上注册它如下
REGASM /CODEBASE TESTCOM.DLL
From a simple VB6 app this works fine
从一个简单的 VB6 应用程序这工作正常
Private Sub Form_Load()
Dim o As Object
Set o = CreateObject("Test9.COMINT")
o.Init "A", "B"
End Sub
This exact same code called from VBA in Excel gives
这个从 Excel 中的 VBA 调用的完全相同的代码给出
"automation error" (0x80131700)
“自动化错误”(0x80131700)
Everything works fine on a development machine, just not on a production machine with just .NET and MS Office installed.
在开发机器上一切正常,只是在只安装了 .NET 和 MS Office 的生产机器上运行良好。
Update
更新
I think this is something to do with the .NET framework not being initialized properly, when running under Excel. If I use Filemon I can see it skip around looking for MSCORWKS.DLL. When I call the same object from VBScript, it finds MSCorwks.dll fine.
我认为这与 .NET 框架在 Excel 下运行时未正确初始化有关。如果我使用 Filemon,我可以看到它跳过查找 MSCORWKS.DLL。当我从 VBScript 调用同一个对象时,它发现 MSCorwks.dll 很好。
When I called CorBindToCurrentRunTime
from VBA to try to forcibly load the CLR, interestingly I get the exact same HRESULT (0x80131700)
as when I do CreateObject()
in VBA.
当我CorBindToCurrentRunTime
从 VBA调用尝试强制加载 CLR 时,有趣的是我得到的结果与在 VBA 中HRESULT (0x80131700)
所做的完全相同CreateObject()
。
Therefore I think it is a framework initialization issue.
因此我认为这是一个框架初始化问题。
回答by Vyas Bharghava
This works for me from VBA... I tried it using Word & Excel 2003 (SP3).
这对我从 VBA 工作...我尝试使用 Word & Excel 2003 (SP3)。
I'm not sure what you mean by "production" machine. Because this is a "client" application and must be executed on the client using Excel.
我不确定您所说的“生产”机器是什么意思。因为这是一个“客户端”应用程序,必须使用 Excel 在客户端上执行。
If you're automating Excel on the server and triggering this "interop" through a VBA call, you're asking for trouble :)
如果您在服务器上自动化 Excel 并通过 VBA 调用触发此“互操作”,那么您就是在自找麻烦:)
Assuming that by production, you mean the client machine where user will be using the Excel template / doc, these are the following pointers:
假设通过生产,您的意思是用户将使用 Excel 模板/文档的客户端机器,这些是以下指针:
- Make sure you have the appropriate .Net framework
- That you have latest service packs for Office
- Try ascertain if this is a rights issue.
- 确保你有合适的 .Net 框架
- 你有最新的 Office 服务包
- 尝试确定这是否是权利问题。
If you're feeling adventuristic, you could use a process explorer [from Microsoft sysinternals site] to see what're the DLLs loaded and where exactly are you getting the error and compare it to the list on your dev box.
如果您喜欢冒险,您可以使用进程浏览器 [来自 Microsoft sysinternals 站点] 来查看加载的 DLL 是什么以及您在哪里收到错误并将其与开发框中的列表进行比较。
Hope this helps.
希望这可以帮助。
回答by JoshBerke
RC1, I tested this with your code from VBScript and from within Office 2007's Excel, everything works fine.
RC1,我使用您的 VBScript 代码和 Office 2007 的 Excel 中的代码对此进行了测试,一切正常。
Since your able to create the COM object from within a VB6 form we should assume that your .net framework is ok. Can you rule out issues with VBA? Can you create a .vbs file and put this in it:
由于您能够从 VB6 表单中创建 COM 对象,我们应该假设您的 .net 框架没问题。你能排除 VBA 的问题吗?你能创建一个 .vbs 文件并将其放入其中吗:
Dim o As Object
Set o = CreateObject("Test9.COMINT")
o.Init "A", "B"
Save the file and double click it. If you get an error, then I would think there is an issue with it being registered, if you don't get an error, then I would look at Office and VBA and see if something is missing or not installed properly.
保存文件并双击它。如果您收到错误消息,那么我会认为它的注册存在问题,如果您没有收到错误消息,那么我会查看 Office 和 VBA,看看是否缺少某些内容或未正确安装。
Another option is to add a reference to COM object and use early binding? I think you might need to export a typelibrary first, but you should be able to add a reference and simple new the object up.
另一种选择是添加对 COM 对象的引用并使用早期绑定?我认为您可能需要先导出类型库,但您应该能够添加引用并简单地新建对象。
回答by JoshBerke
I'm going to answer my own question, hopefully to spare others the hours of tedious drudgery I have just endured.
我将回答我自己的问题,希望能让其他人免于我刚刚忍受的单调乏味的苦差事。
If you get this, it isbecause the .NET based COM assembly can't find the .NET framework
如果你得到这个,那是因为基于 .NET 的 COM 程序集找不到 .NET 框架
The solution is simple. Create a file containing the following
解决方法很简单。创建一个包含以下内容的文件
<?xml version="1.0"?>
<configuration>
<startup>
<supportedRuntime version="v2.0.50727"/>
</startup>
</configuration>
Call it "Excel.Exe.Config" and place it in the same directory as "EXCEL.EXE"
将其命名为“Excel.Exe.Config”并将其放在与“EXCEL.EXE”相同的目录中
Problem solved!
问题解决了!
回答by ryang
installing the follow fix will resolve this issue http://www.microsoft.com/downloads/details.aspx?FamilyID=1b0bfb35-c252-43cc-8a2a-6a64d6ac4670&displaylang=en
安装以下修复程序将解决此问题 http://www.microsoft.com/downloads/details.aspx?FamilyID=1b0bfb35-c252-43cc-8a2a-6a64d6ac4670&displaylang=en
回答by Robino
rc1 is correct in that this isa .net error, thrown when Office can't decide which version of the Framework to use. However, Office isn't throwing a wobbly simply because it's spoilt for choice. There is a bug in how Office 2003 interacts with .net 2.0.
rc1 是正确的,因为这是一个 .net 错误,当 Office 无法决定使用哪个版本的框架时抛出。然而,Office 并不会仅仅因为它被宠坏了选择而摇摆不定。Office 2003 与 .net 2.0 的交互方式存在错误。
Installing the fix from Microsoft (KB908002) is a more flexible way of solving the problem than by forcing Excel to run in a particular version of .net.
与强制 Excel 在特定版本的 .net 中运行相比,安装来自 Microsoft ( KB908002)的修复程序是一种更灵活的解决问题的方法。
See also: http://www.biopdf.com/guide/trouble_shoot_microsoft_office_2003.php
另见:http: //www.biopdf.com/guide/trouble_shoot_microsoft_office_2003.php