C++ 当实例化 ActiveX 控件时,是什么导致 Excel 中的 Visual Basic 运行时错误 -2147319765 (8002802b)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/569950/
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
What causes Visual Basic Run-time error -2147319765 (8002802b) in Excel when an ActiveX control has been instanced?
提问by Ashley Davis
I have created an ActiveX control using C++. I use Visual Basic code to instance the control in an Excel worksheet. I can only run the VB script once, subsequent runs cause the following runtime error when attempting to access the 'ActiveSheet' variable:
我使用 C++ 创建了一个 ActiveX 控件。我使用 Visual Basic 代码在 Excel 工作表中实例化控件。我只能运行一次 VB 脚本,随后的运行在尝试访问“ActiveSheet”变量时会导致以下运行时错误:
Microsoft Visual Basic
Run-time error '-2147319765 (8002802b)':
Automation error
Element not found
I am trying to work out what causes this error and how I can fix it?
我正在尝试找出导致此错误的原因以及如何修复它?
As an experiment I tried creating a simple ActiveX control generated by Visual Studio wizards (in both VS 2005 & 2008). I didn't add or modify any code in this test case. The simple test case still causes this error.
作为一项实验,我尝试创建一个由 Visual Studio 向导(在 VS 2005 和 2008 中)生成的简单 ActiveX 控件。我没有在这个测试用例中添加或修改任何代码。简单的测试用例仍然会导致此错误。
Other ActiveX controls in the system don't cause this error (eg I tried instancing 'Bitmap Image') from VB code.
系统中的其他 ActiveX 控件不会从 VB 代码导致此错误(例如,我尝试实例化“位图图像”)。
This is the VB code (a macro that I recorded, but hand-coded VB has the same issue):
这是 VB 代码(我录制了一个宏,但手动编码的 VB 也有同样的问题):
Sub Macro1()
ActiveSheet.OLEObjects.Add(ClassType:="test.test_control.1" _
, Link:=False, DisplayAsIcon:=False).Select
End Sub
Can anyone give me an answer on this? Alternatively any pointers to resources that may help will be appreciated.
任何人都可以给我一个答案吗?或者,任何指向可能有帮助的资源的指针将不胜感激。
Thanks
谢谢
采纳答案by Ashley Davis
After talking to Microsoft I found out the cause of the problem I was having.
在与微软交谈后,我发现了我遇到的问题的原因。
When creating an ActiveX control using the VS 2005/2008 wizard you need to check the 'Connection points' check box in the 'Options' page. This adds, among other things, IConnectionPointContainerImpl as a base class for your ATL class, which in turn implements IConnectionPointContainer.
使用 VS 2005/2008 向导创建 ActiveX 控件时,您需要选中“选项”页面中的“连接点”复选框。这增加了 IConnectionPointContainerImpl 作为 ATL 类的基类,而 ATL 类又实现了 IConnectionPointContainer。
Failure to do this means that you can't insert your ActiveX control into an Excel document via Visual Basic more than once. The second time you execute the script you start getting the 'automation errors'.
不这样做意味着您不能多次通过 Visual Basic 将 ActiveX 控件插入到 Excel 文档中。第二次执行脚本时,您开始收到“自动化错误”。
The answer to the problem was simple enough and it worked, although I am still not sure how it actually relates to the 'automation error' and leaves me wondering why the error messages are not more informative.
问题的答案很简单并且有效,尽管我仍然不确定它与“自动化错误”的实际关系,并且让我想知道为什么错误消息没有更多信息。
回答by Mike Rosenblum
You have created an "unqualified" reference to an Excel application that you cannot release by utilizing a Global variable intended for VBA that should not be used in VB 6.0.
您创建了对 Excel 应用程序的“非限定”引用,您无法通过使用不应在 VB 6.0 中使用的 VBA 全局变量释放该引用。
This is an unfortunate side-effect of using VB 6.0, but it is the only problem I know of using VB6, and it is easily fixed.
这是使用 VB 6.0 的一个不幸的副作用,但这是我所知道的使用 VB6 的唯一问题,而且很容易修复。
The problem in your case stems from using the 'ActiveSheet' global variable. When using VBA, this is fine, but when using VB 6.0, you must avoid this or else you create an Excel application that you cannot release. This approach will run fine the first time, but will cause all kinds of undefined behavior the secondtime your routine runs.
您案例中的问题源于使用“ActiveSheet”全局变量。使用 VBA 时,这很好,但使用 VB 6.0 时,您必须避免这种情况,否则您将创建一个无法发布的 Excel 应用程序。这种方法第一次运行良好,但第二次运行例程时会导致各种未定义的行为。
In your example, the code should do something like this:
在您的示例中,代码应执行以下操作:
Sub Macro1()
Dim xlApp As Excel.Application
Set xlApp = New Excel.Application
xlApp.ActiveSheet.OLEObjects.Add(ClassType:="test.test_control.1" _
, Link:=False, DisplayAsIcon:=False).Select
' Then when done:
xlApp.Quit()
xlApp = Nothing
End Sub
For a detailed discussion about how to handle this in general, see:
有关一般如何处理此问题的详细讨论,请参阅:
VB 6.0 Tutorial - Finding and Repairing Unqualified References (http://www.xtremevbtalk.com/showthread.php?p=900556#post900556)
VB 6.0 教程 - 查找和修复不合格的引用 ( http://www.xtremevbtalk.com/showthread.php?p=900556#post900556)
For Microsoft documentation on this issue see:
有关此问题的 Microsoft 文档,请参阅:
Excel Automation Fails Second Time Code Runs (MSKB 178510) (http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/q178/5/10.asp)
Excel 自动化第二次代码运行失败 (MSKB 178510) ( http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/q178/5/ 10.asp)
Error or Unexpected Behavior with Office Automation When You Use Early Binding in Visual Basic (MSKB 319832) (http://support.microsoft.com/default.aspx?scid=KB;EN-US;Q319832&)
在 Visual Basic 中使用早期绑定时 Office 自动化的错误或意外行为 (MSKB 319832) ( http://support.microsoft.com/default.aspx?scid=KB;EN-US;Q319832&)
Edit: Note that using html 'a' tags were not working with these links for some reason. Someone might need to look into the parser?
编辑:请注意,由于某些原因,使用 html 'a' 标签不适用于这些链接。有人可能需要查看解析器?
回答by Ant
I had this error with UserControl classes that were implementing interfaces manually, but with no events. The solution in this case was to declare an empty interface for the ComSourceInterfaces attribute:
我在手动实现接口的 UserControl 类中遇到了这个错误,但没有事件。这种情况下的解决方案是为 ComSourceInterfaces 属性声明一个空接口:
[ComVisible( true )]
[Guid( "your-guid-here" )]
[ProgId( "progid-here" )]
[ComDefaultInterface( typeof( IMyClass ) )]
[ComSourceInterfaces( typeof( IMyClassEvents ) )]
[ClassInterface( ClassInterfaceType.None )]
public partial class ExcelMap : UserControl, IMyClass
{
//implementation here
}
[ComVisible( true )]
[Guid( "your-2nd-guid" )]
[InterfaceType( ComInterfaceType.InterfaceIsDual )]
public interface IMyClass
{
//whatever things you need to define here
}
[ComVisible( true )]
[Guid( "your-3rd-guid" )]
[InterfaceType( ComInterfaceType.InterfaceIsDual )]
public interface IMyClassEvents
{
//empty interface to avoid Automation errors
//you can define events here in the normal way, but it's not necessary
}
回答by Andrew Cowenhoven
回答by Toby Allen
From the error description and guessing a bit I would suggest you try the following.
根据错误描述和猜测,我建议您尝试以下操作。
- Make sure you can instantiate your ActiveX control elsewhere without problems. This might simply be an issue with instantiation of your ActiveX and not an Excel issue.
- 确保您可以在其他地方实例化您的 ActiveX 控件而不会出现问题。这可能只是 ActiveX 实例化的问题,而不是 Excel 问题。
Also on a related now, you should use the general "test.test_control" rather than "test.test_control.1" but thats not your issue here.
同样在相关的现在,您应该使用一般的“test.test_control”而不是“test.test_control.1”,但这不是您的问题。