【问题标题】:How can I fix "Unable to cast COM object of type 'Microsoft.Office.Interop.Outlook.ApplicationClass' / The RPC server is unavailable"?如何修复“无法转换类型为 'Microsoft.Office.Interop.Outlook.ApplicationClass' / RPC 服务器不可用的 COM 对象”?
【发布时间】:2017-02-08 16:31:18
【问题描述】:

我收到上述错误,当在特定机器上运行时,它会暂停我的应用程序中的活动。当我在自己的机器上运行它时,不会出现这样的错误。

也许“RPC 服务器不可用”是问题的症结所在,但是在应用程序之前运行(并且仍在我的机器上运行)之后会弹出这个问题的原因是什么?

错误消息,在更多上下文中(显示似乎有价值/重要的内容)是:

System.InvalidCastException:无法将类型为“Microsoft.Office.Interop.Outlook.ApplicationClass”的 COM 对象转换为接口类型“Microsoft.Office.Interop.Outlook._Application”。此操作失败,因为 IID 为“{00063001-0000-0000-C000-000000000046}”的接口的 COM 组件上的 QueryInterface 调用因以下错误而失败:RPC 服务器不可用。 (来自 HRESULT 的异常:0x800706BA)。 在 System.StubHelpers.StubHelpers.GetCOMIPFromRCW(对象 objSrc,IntPtr pCPCMD,IntPtr& ppTarget,布尔& pfNeedsRelease) 在 Microsoft.Office.Interop.Outlook.ApplicationClass.CreateItem(OlItemType ItemType) 在 RoboReporter2017.ExceptionLoggingService.EmailMessageToAssignee(字符串单元,字符串 notificationRecipient,字符串 rptName) 在 RoboReporter2017.RoboRprtrLib.GenerateAndSaveDueReports() 在 RoboReporter2017.FormMain.RunDueReports() 在 RoboReporter2017.FormMain.FormMain_Load(对象发送者,EventArgs e) . . .

************** 加载的程序集 ************** -------------------------------------- Microsoft.Office.Interop.Outlook 程序集版本:12.0.0.0 Win32版本:12.0.4518.1014 代码库:file:///C:/Windows/assembly/GAC/Microsoft.Office.Interop.Outlook/12.0.0.0__71e9bce111e9429c/Microsoft.Office.Interop.Outlook.dll -------------------------------------- 办公室 程序集版本:12.0.0.0 Win32版本:12.0.4518.1014 代码库:file:///C:/Windows/assembly/GAC/office/12.0.0.0__71e9bce111e9429c/office.dll ------------------------------------------

如 err msg 中所引用的那样,在该机器上发生故障的方法是:

internal static void EmailMessageToAssignee(string unit, string notificationRecipient, string rptName)
{
    string saveLocation = @"\\storageblade\cs\REPORTING\RoboReporter";
    var subject = string.Format("Your {0} report for {1} generated by Robo Reporter 2017", rptName, unit);
    var body = string.Format("Your {0} report for {1} was generated by Robo Reporter 2017 and can be found in the usual location in the shared network folder ({2})", rptName, unit, saveLocation);

    Application app = new Application();
    MailItem mailItem = app.CreateItem(OlItemType.olMailItem);
    mailItem.To = notificationRecipient;
    mailItem.Subject = subject;

    mailItem.HTMLBody = string.Format(@"<html><body><img src='http://www.proactusa.com/bla/images/pa_logo_notag.png' alt='Platypus logo' width='199' height='130' ><p>{0}</p></body></html>", body);

    mailItem.Importance = OlImportance.olImportanceNormal;
    mailItem.Display(false);
    mailItem.Send();
}

我注意到我的项目参考中的 Microsoft.Office.Interop.Outlook 版本是 12.0.0.0,与错误消息中列出的“加载的程序集”中列出的版本相同。

更新

可能是 Outlook 没有运行是问题所在,我编写了以下代码:

private static void StartOutlookIfNotRunning()
{
    string OutlookFilepath = @"C:\Program Files (x86)\Microsoft 
Office\Office12\OUTLOOK.EXE";
    if (Process.GetProcessesByName("OUTLOOK").Count() > 0) return;
    Process process = new Process();
    process.StartInfo = new ProcessStartInfo(OutlookFilepath);
    process.Start();
}

...改编自here,但在实施之前,我关闭了 Outlook 并运行了应用程序,看看如果 Outlook 没有运行,我是否会在我的机器上收到相同的错误消息。但不是!它会自行重启 Outlook,不需要我花哨的 StartOutlookIfNotRunning() 方法。

所以这不是问题,无论如何......

【问题讨论】:

  • 是的,Outlook 2007 安装在那里(和我的机器上一样);但它可能没有运行...

标签: c# office-interop assemblies com-interop comobject


【解决方案1】:

FIX: Error message when you run an application that makes "burst load" style activation requests and calls a server component through DCOM: "0x800706ba" or "0x800706bf"

您在何时何地尝试自动化 Outlook?

Microsoft 目前不推荐也不支持任何无人值守、非交互式客户端应用程序或组件(包括 ASP、ASP.NET、DCOM 和 NT 服务)的 Microsoft Office 应用程序自动化,因为 Office 可能表现出不稳定Office 在此环境中运行时出现的行为和/或死锁。

如果您要构建在服务器端上下文中运行的解决方案,您应该尝试使用已确保无人值守执行安全的组件。或者,您应该尝试找到允许至少部分代码在客户端运行的替代方案。如果您使用服务器端解决方案中的 Office 应用程序,该应用程序将缺少许多成功运行所需的功能。此外,您将承担整体解决方案稳定性的风险。在Considerations for server-side Automation of Office 文章中阅读更多相关信息。

【讨论】:

  • 当某个进程运行时,我帖子中显示的电子邮件代码会循环触发。由于 Outlook 是我们使用的电子邮件引擎,因此我必须“自动化”它,而不是通过其他服务发送电子邮件。
【解决方案2】:

好吧,虽然 Eugene Astafiev 的建议很中肯,但有两次我与不祥之物发生碰撞

System.InvalidCastException:无法将类型为“Microsoft.Office.Interop.Outlook.ApplicationClass”的 COM 对象转换为接口类型“Microsoft.Office.Interop.Outlook._Application”。

我通过 another forum 中 Eugene Astafiev 的建议解决了这个问题:

regtlib msoutl.olb

来自 Office App 文件夹中提升的命令提示符。

对我来说,关键是在某些机器上正常工作,而在另一台机器上却不工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-02
    • 1970-01-01
    • 1970-01-01
    • 2017-11-10
    • 1970-01-01
    • 2016-02-02
    相关资源
    最近更新 更多