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

Error loading Office.dll COMException: 'Errorloading type library/DLL'

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

public bool IsEditMode(Microsoft.Office.Interop.Excel.Application xlApp)
    {
        //
        //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。

我的版本号:

我试过的参考文献

所有这三个参考文献都给了我同样的例外。关于如何加载这个有什么想法吗?

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

问题出在不同版本 Office 的类型库注册上。我最终删除了所有不再安装的 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

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

对我有用的解决方案是从

中的注册表中删除 Win32 条目
Computer\HKEY_CLASSES_ROOT\TypeLib\{00020813-0000-0000-C000-000000000046}.9[=10=]

因为 Data 指的是无效路径

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

注意: 请通过 Export 保留条目的备份,以防出现问题。