【问题标题】:Disable COM-Addin through code通过代码禁用 COM-Addin
【发布时间】:2017-12-12 10:50:47
【问题描述】:

在我们公司内,我们有 2 个插件在 Excel 中无法相处。其中一个插件来自 SAP(例如无法修改)。另一个是使用Add In Express开发的本地的

问题仅限于 Excel。当我们尝试从受保护的视图中解除文档时。使用以下代码

ExcelApp.ActiveProtectedViewWindow.Edit()

当我们执行那行代码时,另一个 (SAP) 插件开始抛出访问冲突。

1st error: Attempted to read or write protected memory. This is often an indication that other memory is corrupt
2nd error: Exception from HResult 0x800A03EC

现在我正在尝试解决冲突。但我被困住了。 我找不到另一种方法可以成功地将文档从受保护的视图中取出

暂时禁用 SAP 插件似乎也不起作用。因为Application.Addins 不包含 COM-Addins。而Application.CommAddins 则抛出以下错误:

       ExcelApp.COMAddIns   The embedded interop type 'COMAddIns' does not contain a 
       definition for'Microsoft.Office.Interop.Excel._Application' since it was not used 
       in the compiled assembly. Consider casting to object or changing the 'Embed Interop Types' 
       property to true.    

然而Embed Interop Types 设置为真。

那么有人有什么想法吗?

注意 禁用Protected view 不是一种选择。因为它是在公司层面决定的:-(

【问题讨论】:

  • 如果您使用 com 插件的 progid,对象模型应该可以工作 - 然后您应该能够将 Connect 属性设置为 False 以断开连接。
  • @CharlesWilliams 知道他为什么会抱怨嵌入互操作类型吗?
  • 这可能是 Interop 中的一个错误 - 尝试从 VBA 执行此操作 - VBA 对象模型似乎工作正常。请注意,如果为所有用户(可能是)安装了 COM 插件,我不确定您是否可以使用自动化断开它,因为断开连接需要管理员权限。
  • @CharlesWilliams 感谢您的意见。值得一试:)

标签: .net excel vb.net excel-interop


【解决方案1】:

我已经设法解决了这个问题。感谢Charles Williams 使用以下代码,我可以使用 vb.code 禁用/启用某些 COM-Addins

在这种情况下,SapExcelAddIn 是导致我们所有问题的插件。

For Each certAddin As Object In ExcelApp.COMAddIns
      log("progId:" & certAddin.progId) 'Logging the id of all Com-addins
      If (certAddin.progId.Equals("SapExcelAddIn")) Then
           log("disconnecting:" & certAddin.progId)
           certAddin.Connect = False
      End If
Next

【讨论】:

    【解决方案2】:

    以防万一有人想在 C# 中执行此操作,这里有一些代码可以禁用多个 COM-AddIn,通过数组参数按名称指定:

    private void TryDisableCommAddIns(Microsoft.Office.Interop.Excel.Application app, string[] addInsToDisable)
            {
                if(addInsToDisable == null)
                {
                    return;
                }
                COMAddIns comAddIns = app.COMAddIns;
                foreach (string addInName in addInsToDisable)
                {
                    //If you have a logger you could log a debug statement such as: $"Disabling COM AddIn: {addInName}"
                    COMAddIn commAddInOrNull = GetCommAddInOrNull(comAddIns, addInName);
                    if(commAddInOrNull != null)
                    {
                        commAddInOrNull.Connect = false;
                    }
                }
            }
    
            private COMAddIn GetCommAddInOrNull(COMAddIns comAddIns, string addInName)
            {
                try
                {
                    return comAddIns.Item(addInName);
                }
                catch (Exception)
                {
                    //If you have a logger you could log a debug statement such as: $"No COM AddIn found with name: {addInName}"
                    return null;
                }
            }
    

    【讨论】:

      猜你喜欢
      • 2011-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-08
      • 2012-06-09
      • 1970-01-01
      • 2017-09-10
      • 1970-01-01
      相关资源
      最近更新 更多