【发布时间】: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