使用COM时Excel加载项回调出现静默失败问题排查
Excel加载项开发:COM接口调用问题及静默失败排查
环境:Visual Studio 2022 + Excel 365,此前使用ExcelDna完成多项任务,直接调用Excel COM接口时遇到以下问题:
初始按钮与回调正常运行
在Custom UI XML中定义按钮:<button id='button_sample' label='Sample' onAction='OnSample' />对应的简单回调函数可正常执行,能输出调试信息:
public void OnSample(IRibbonControl control) { Debug.WriteLine("Starting sample..."); }引入Interop语句报错
扩展函数需获取用户选区,添加interop程序集后,执行引入语句:using Excel = Microsoft.Office.Interop.Excel;出现编译错误:
The type or namespace name 'Office' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)添加COM引用后编译失败
通过「添加COM引用」选择「Microsoft Excel 16.0 Object Library」,编译时出现错误:Could not determine the dependencies of the COM reference "Microsoft.Office.Interop.Excel". Error loading type library/DLL. (Exception from HRESULT: 0x80029C4A (TYPE_E_CANTLOADLIBRARY))改用15.0版本Interop后的静默失败
改用GAC中的15.0版本Microsoft.Office.Interop.Excel.dll后,编译无错误,但OnSample函数静默失败——既无调试输出,也未抛出任何异常。已尝试的无效方案
- 引用NuGet包
MSOffice.Interop 16.*:出现兼容性警告,静默失败问题仍存在 - 安装Visual Studio的VSTO组件:未解决16.0对象库加载错误
- 引用NuGet包
现寻求解决该静默失败问题的可行方案。
内容的提问来源于stack exchange,提问作者chrisaycock
相关产品推荐
相关产品推荐

