如何从 CommandBars.ExecuteMso 调试 E_FAIL?

How to debug E_FAIL from CommandBars.ExecuteMso?

在使用我的 PowerPoint 加载项时,一些用户遇到以下异常: Error HRESULT E_FAIL has been returned from a call to a COM component. 尝试使用 Application.CommandBars.ExecuteMso("FileNew") 创建新演示文稿时会发生这种情况,这应该会打开后台选项卡。

并发症:

  1. 后台支持用户(PowerPoint 2016)
  2. Microsoft 官方文档仅说明 E_FAIL 的 return 以防失败,未提供 C# as well as for VBA
  3. 的进一步线索
  4. 我无法在我可用的任何机器上重现该问题

如何调试并尝试防止此异常?

感谢 Hans Passant 重述了错误代码 "E_Fail for controls that are not enabled or not visible",我仔细检查了 CommandBars 对象,发现有两种方法:

  1. Application.CommandBars.GetEnabledMso(CommandName)
  2. Application.CommandBars.GetVisibleMso(CommandName)

所以我用的是第一个,在执行命令之前检查命令是否启用

如果要激活特定的内置选项卡,您需要使用 IRibbonUI 界面的 ActivateTabMso。然后你就可以使用ExecuteMso方法了。

您可以在 Ribbon Extensibility in Office 2010: Tab Activation and Auto-Scaling

中阅读更多相关信息