【问题标题】:How do I avoid RPC_E_CALL_REJECTED exceptions when performing PowerPoint automation?执行 PowerPoint 自动化时如何避免 RPC_E_CALL_REJECTED 异常?
【发布时间】:2012-08-30 02:15:03
【问题描述】:

当我的代码尝试创建 Microsoft.Office.Interop.PowerPoint.Application 的新实例时,有时会出现以下异常:

System.Runtime.InteropServices.COMException (0x80010001): Retrieving the COM class factory for component with CLSID {91493441-5A91-11CF-8700-00AA0060263B} failed due to the following error: 80010001 Call was rejected by callee. (Exception from HRESULT: 0x80010001 (RPC_E_CALL_REJECTED)).
   at System.Runtime.Remoting.RemotingServices.AllocateUninitializedObject(RuntimeType objectType)
   at System.Runtime.Remoting.Activation.ActivationServices.CreateInstance(RuntimeType serverType)
   at System.Runtime.Remoting.Activation.ActivationServices.IsCurrentContextOK(RuntimeType serverType, Object[] props, Boolean bNewObj)
   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic)

我说有时是因为即使给出相同的输入,它也不会始终如一地发生。此外,它也发生在我也与 PowerPoint 自动化 API 交互的代码的其他部分(同样缺乏一致性)。

我已经尝试了来自 MSDN 本身的 this 解决方案,这似乎是最推荐的解决方案。但是,它似乎没有任何影响,因为我仍然观察到相同的行为。

我的问题是:

  1. MSDN 解决方案是否适用于 PowerPoint 自动化?
  2. 如何验证我是否已将其正确应用到我的代码中?
  3. 有人有替代解决方案吗?

我正在使用 C#、.NET 4 和 PowerPoint 2007。

【问题讨论】:

  • 我遇到了同样的问题。也许在这里和那里添加一个 Thread.Sleep(100) 应该有帮助?填充图表需要一些时间来更新图形等,因此在添加更多数据之前您可能需要等待。

标签: c# powerpoint office-interop comexception


【解决方案1】:
  1. 我没有尝试过,但 Andrew Whitechapel 描述了 Office 的相同方法,所以我想它应该可以工作:http://blogs.msdn.com/b/andreww/archive/2008/11/19/implementing-imessagefilter-in-an-office-add-in.aspx

  2. 试试看 :)

  3. 另一种方法是通过捕获错误并重试来实现某种等待机制(也提到了here)。

【讨论】:

  • 谢谢,你链接到的第一篇文章很好地解释了IMessageFilter
【解决方案2】:

我之前遇到过这个问题,Paul B 确实是正确的。这取决于您是否从主线程(即This_AddIn)调用Powerpoint OM。如果是,ppt 不应该抛出这些异常。但是,如果您从另一个线程调用 ppt,则必须实现 IMessageFilter 以有效地处理这些 windows 消息泵错误,因为 ppt 优先于来自其他线程的调用对 OM 的主线程调用,因此调用被拒绝。

还有一个警告需要进一步的样板代码来处理额外的COMExceptions,例如0x800AC472 (VBA_E_IGNORE)。有一个例子here

因此,完整的解决方案是实现IMessageFilter 并使用sepp2k's 之类的代码来包装您的OM 调用,以处理可能抛出的其他类型的COMException

所以,像他这样包装代码:

private void TryUntilSuccess(Action action)
{
    bool success = false;
    while (!success)
    {
        try
        {
            action();
            success = true;
        }

        catch (System.Runtime.InteropServices.COMException e)
        {
            if ((e.ErrorCode & 0xFFFF) == 0xC472)
            {   // Excel is busy
                Thread.Sleep(500); // Wait, and...
                success = false;  // ...try again
            }
            else
            {   // Re-throw!
                throw e;
            }
        }
    }
}

你可以像这样用 lamdas 调用它:

TryUntilSuccess(() =>
{
    RegisterFilter(); // register this thread for IMessageFilter use
    ppt_app.DoSomething();        
    UnRegisterFilter(); // unregister this thread for IMessageFilter use
};)

这种两管齐下的方法的原因是IMessageFilter 策略比抛出异常更有效,并且比无法处理来自应用程序的繁忙消息的次数更多。然而,在其他时候你将不得不处理异常,所以你必须同时做......

请参阅此处了解包含包装器的 IMessageFilter implementation

嗯!

【讨论】:

  • 只是'扔;'没有 e 异常重新抛出而不会丢失堆栈跟踪。 Exception Handling
猜你喜欢
  • 2012-07-09
  • 2015-12-11
  • 2011-07-16
  • 1970-01-01
  • 2017-02-10
  • 1970-01-01
  • 1970-01-01
  • 2010-09-29
  • 1970-01-01
相关资源
最近更新 更多