繁体   English   中英

如何修复“无法转换类型为‘Microsoft.Office.Interop.Outlook.ApplicationClass’的 COM 对象/RPC 服务器不可用”?

[英]How can I fix “Unable to cast COM object of type 'Microsoft.Office.Interop.Outlook.ApplicationClass' / The RPC server is unavailable”?

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

也许“ 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(Object objSrc, IntPtr pCPCMD, IntPtr& ppTarget, Boolean& pfNeedsRelease) 在 Microsoft.Office.Interop.Outlook.ApplicationClass.CreateItem(OlItemType ItemType) 在 RoboReporter2017.mailExceptionLoggingStringAssignenotification(Object objSrc, IntPtr pCPCMD, Boolean& pfNeedsRelease) String rptName) 在 RoboReporter2017.RoboRprtrLib.GenerateAndSaveDueReports() 在 RoboReporter2017.FormMain.RunDueReports() 在 RoboReporter2017.FormMain.FormMain_Load(Object sender, 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 --------------- ----------------- office 程序集版本: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,与 err msg 中列出的“加载的程序集”中列出的版本相同。

更新

考虑到 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() 方法。

所以这不是问题,反正...

请参阅FIX:当您运行发出“突发加载”样式激活请求并通过 DCOM 调用服务器组件的应用程序时的错误消息: "0x800706ba" 或 "0x800706bf"

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

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

如果您正在构建一个在服务器端上下文中运行的解决方案,您应该尝试使用已为无人值守执行安全的组件。 或者,您应该尝试找到至少允许部分代码在客户端运行的替代方案。 如果您使用服务器端解决方案中的 Office 应用程序,该应用程序将缺少许多成功运行所需的功能。 此外,您将面临整体解决方案稳定性的风险。 Office 服务器端自动化注意事项一文中阅读有关此内容的更多信息。

好吧,虽然 Eugene Astafiev 的建议非常合理,但在我两次与不祥之兆相撞的情况下

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

我在另一个论坛上用 Eugene Astafiev 的建议解决了这个问题

regtlib msoutl.olb

从 Office App 文件夹内提升的命令提示符。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM