加载 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。
我的版本号:
- VS 2017 社区 (15.8.6)
- Excel 2010 Office 标准版 32 位 (14.0.7214.5000)
我试过的参考文献
- 手动添加对C:\Windows\assembly\GAC_MSIL\office.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.0.0.0__71e9bce111e9429c\Office.dll)
所有这三个参考文献都给了我同样的例外。关于如何加载这个有什么想法吗?
您是否尝试过将 "Microsoft.Office.Interop.Excel.Application" 更改为动态?这应该可以解决您的库版本问题。但是,您将失去该对象的智能感知。先来个淘汰赛吧
问题出在不同版本 Office 的类型库注册上。我最终删除了所有不再安装的 Office 版本的注册表项。
这些链接为我提供了让它正常工作所需的信息。
我在从 32 位版本的 Office 升级到 64 位版本时遇到了这个错误,这不知何故导致 Win32
和 Win64
条目同时存在于注册表中。
对我有用的解决方案是从
中的注册表中删除 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
保留条目的备份,以防出现问题。
我有一个可用的 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。
我的版本号:
- VS 2017 社区 (15.8.6)
- Excel 2010 Office 标准版 32 位 (14.0.7214.5000)
我试过的参考文献
- 手动添加对C:\Windows\assembly\GAC_MSIL\office.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.0.0.0__71e9bce111e9429c\Office.dll)
所有这三个参考文献都给了我同样的例外。关于如何加载这个有什么想法吗?
您是否尝试过将 "Microsoft.Office.Interop.Excel.Application" 更改为动态?这应该可以解决您的库版本问题。但是,您将失去该对象的智能感知。先来个淘汰赛吧
问题出在不同版本 Office 的类型库注册上。我最终删除了所有不再安装的 Office 版本的注册表项。
这些链接为我提供了让它正常工作所需的信息。
我在从 32 位版本的 Office 升级到 64 位版本时遇到了这个错误,这不知何故导致 Win32
和 Win64
条目同时存在于注册表中。
对我有用的解决方案是从
中的注册表中删除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
保留条目的备份,以防出现问题。