【问题标题】:VSTO Excel add-in throwing exceptionVSTO Excel 加载项引发异常
【发布时间】:2014-06-01 08:02:21
【问题描述】:

我正在使用 VSTO for Excel 2007 并使用 IConnectionPointContainer 管理 Excel 应用程序事件,而不是直接订阅它们。现在在 Office 2013 之前,我没有使用 VSTO,而是实现了 IExtensibility2 接口。我以前直接获取excel应用程序对象,管理excel事件如下:

IConnectionPoint _eventSourceConnectionPoint;
ComObjLifetimeManager _eventSourceConnectionPointLifeManager;

var _eventSource = excelApplication as IConnectionPointContainer;
_eventSource.FindConnectionPoint(ref _xlAppEventsIID, out _eventSourceConnectionPoint);
_eventSourceConnectionPointLifeManager = new ComObjLifetimeManager(_eventSourceConnectionPoint);
_eventSourceConnectionPoint.Advise(this, out _connectionCookie);

这在以前可以正常工作。现在我正在使用 VSTO,我在调用 _eventSource.FindConnectionPoint() 方法时遇到了 TargetInvocationException 异常。

"接口不支持后期绑定调用,因为它不是从 IDispatch 派生的"

除了我现在从“this.Application”获取 excel 应用程序对象,而不是通过 IExtensibility2 接口免费获取之外,其他所有内容都与之前相同。

【问题讨论】:

    标签: excel com vsto excel-addins


    【解决方案1】:

    即使经过大量研究,我也没有得到任何结果,然后我不小心登陆了这里:http://blogs.msdn.com/b/vsod/archive/2010/04/07/how-to-implement-late-bound-event-handling-in-case-of-a-vsto-add-in.aspx
    这解决了我的问题。对于遇到同样问题的其他人,您基本上需要在 assemblyinfo.cs 中执行以下操作:

    1. Set ExcelLocale1033 attribute to false
    2. Mark the assembly as COM Visible
    

    【讨论】:

      猜你喜欢
      • 2021-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-26
      • 1970-01-01
      • 1970-01-01
      • 2017-10-19
      相关资源
      最近更新 更多