c# - 加载 Office.dll 时出错 COMException : 'Errorloading type library/DLL'

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

我有一个工作的 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)

  • 所有这三个引用文献都给了我同样的异常(exception)。任何想法如何加载这个?

    最佳答案

    问题在于来自不同版本 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

    关于c# - 加载 Office.dll 时出错 COMException : 'Errorloading type library/DLL' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53048360/

    相关文章:

    c# - 关于 ASP.Net 中的弹出窗口

    c# - ExtensionMethod 特定于类、属性或方法

    excel - 如何使用 Excel VBA 在多行单元格中的每行最后一个斜杠之后获取单词?

    vba - 替换文本和更改字体颜色

    c++ - 从 VS2013 构建 XP 驱动程序

    visual-studio - 具有共享模型的客户端/服务器项目的最佳 Visual Studio 解决方案/项目结构

    c# - Linq to XML - 尝试打印无穷大符号

    c# - SemaphoreSlim 等待优先级

    excel - 删除所有工作表中除指定列外的所有列

    c++ - Qt 与 Visual Studio 宏问题