0

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

也许“ 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.ExceptionLoggingService.EmailMessageToAssignee(String unit, String notificationRecipient,字符串 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,与错误消息中列出的“已加载程序集”中列出的版本相同。

更新

考虑到 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();
}

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

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

4

2 回答 2

2

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

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

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

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

于 2017-02-20T17:55:57.020 回答
1

好吧,尽管 Eugene Astafiev 的建议非常合理,但有两次我与不祥的人相撞

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

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

regtlib msoutl.olb

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

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

于 2020-11-15T16:01:57.260 回答