【问题标题】:Error loading Office.dll COMException: 'Errorloading type library/DLL'加载 Office.dll 时出错 COMException: 'Errorloading type library/DLL'
【发布时间】:2019-04-02 13:17:24
【问题描述】:

我有一个工作的 Excel COM 库,我正在尝试使用以下方法来确定 excel 是否处于编辑模式

public bool IsEditMode(Microsoft.Office.Interop.Excel.Application xlApp)
    {
        //https://stackoverflow.com/questions/464196/workaround-to-see-if-excel-is-in-cell-edit-mode-in-net
        //xlApp = (Microsoft.Office.Interop.Excel.Application)ExcelDnaUtil.Application;
        var bars = xlApp.Application.CommandBars;
        var commandBar = bars["Worksheet Menu Bar"];
        var menu = commandBar.FindControl(
                   1, //the type of item to look for
                   18, //the item to look for
                   Type.Missing, //the tag property (in this case missing)
                   Type.Missing, //the visible property (in this case missing)
                   true);
        if (menu != null)
        {
            // Check if "New" menu item is enabled or not.
            if (!menu.Enabled)
            {
                return true;
            }
        }
        return false;
    }

当我的代码点击 xlApp.Application.CommandBars;我得到以下异常。

System.Runtime.InteropServices.COMException: 'Error loading type library/DLL. (Exception from HRESULT: 0x80029C4A (TYPE_E_CANTLOADLIBRARY))'

我认为问题是因为我引用了错误版本的 office.dll。要么是针对错误版本的 office,要么是针对错误版本的 Visual Studio。

我的版本号:

  • VS 2017 社区 (15.8.6)
  • Excel 2010 Office 标准版 32 位 (14.0.7214.5000)

我尝试过的参考文献

  • 手动添加对 C:\Windows\assembly\GAC_MSIL\office\14.0.0.0__71e9bce111e9429c\OFFICE.DLL 的引用
  • 让 Visual Studio 自动将引用添加到 C:\Program Files (x86)\Microsoft Visual Studio\Shared\Visual Studio Tools for Office\PIA\Office14\office.dll
  • VS 参考管理器 -> COM -> Microsoft Office 14.0 对象库。 (C:\WINDOWS\assembly\GAC_MSIL\Office\15.0.0.0__71e9bce111e9429c\Office.dll)

所有这三个参考都给了我同样的例外。任何想法如何加载这个?

【问题讨论】:

    标签: c# excel visual-studio com office-interop


    【解决方案1】:

    问题在于来自不同版本的 Office 的 typelib 注册。我最终删除了不再安装的所有 Office 版本的注册表项。

    这些链接为我提供了使其正常工作所需的信息。

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/ac50fa41-8d47-4fa9-81a3-914f262676af/0x80029c4a-typeecantloadlibrary?forum=vsto

    http://kb.palisade.com/index.php?pg=kb.page&id=528

    【讨论】:

      【解决方案2】:

      您是否尝试将“Microsoft.Office.Interop.Excel.Application”更改为动态?这应该可以解决您对库版本的问题。但是,您将失去对象的智能感知。试一试,就像一个先淘汰的过程。

      【讨论】:

      • 还要确保你传递的对象是动态的。
      【解决方案3】:

      自从我从 32 位版本升级到 64 位版本的 Office 后遇到此错误,不知何故导致注册表中同时存在 Win32Win64 条目。

      对我有用的解决方案是从注册表中删除 Win32 条目

      Computer\HKEY_CLASSES_ROOT\TypeLib\{00020813-0000-0000-C000-000000000046}\1.9\0
      

      因为Data 指的是无效路径

      C:\Program Files (x86)\Microsoft Office\Root\Office16\EXCEL.EXE
      

      注意:请通过Export 备份条目,以防万一出现问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-01-25
        • 2018-08-25
        • 2012-03-26
        • 1970-01-01
        • 1970-01-01
        • 2011-01-17
        • 1970-01-01
        相关资源
        最近更新 更多