Excel中的HRESULT代码在C#中互操作

我正在寻找一种干净的方式来在Excel中使用interop来输出C#中的错误。

当文件不是“正确的”时,有一个像“ blabla HRESULT:0x800AC472 ”的消息。

我宁愿要像“ 文件使用有这个或那个问题 ”的消息。 为了做到这一点,我需要知道每个HRESULT映射到哪个错误?

如何获得这样的错误列表?

我看MSDN,发现一些说明,但我似乎无法find像上面提到的“ 0x800AC472 ”所有代码的列表。

我有Excel的HRESULT代码0x800AC472作为VBA_E_IGNORE ,这表明Excel已经“暂停对象浏览器”。

在某些情况下,Excel将拒绝所有传入的COM请求。 发生这种情况的两种情况是用户所在

  • 正忙于编辑一个公式,或者
  • 在Excel工作表上按下鼠标button。

有可能是其他情况 – 例如,我不确定Excel忙于计算时错误代码是什么。

如果您正在使用COM自动化与交互式使用的Excel实例交谈,那么您对Excel进行的任何COM调用都可能会返回此错误。 我使用Excel-DNA (其中interop总是从Excel进程中的加载项发生的)一个方法是尝试调用Application.Run(…)在加载项中运行一个macros。 一旦Application.Run调用成功,macros将运行在主要的Excel线程的上下文中的COM请求将不会由于Excel挂起COM调用失败。

你可以从Excel中得到一些其他的COM错误:

  const uint RPC_E_SERVERCALL_RETRYLATER = 0x8001010A; const uint RPC_E_CALL_REJECTED = 0x80010001; // Not sure when we get this one? // Maybe when trying to get the Application object from another thread, // triggered by a ribbon handler, while Excel is editing a cell. const uint VBA_E_IGNORE = 0x800AC472; // Excel has suspended the object browser const uint NAME_NOT_FOUND = 0x800A03EC; // When called from the main thread, but Excel is busy anyway.