【发布时间】: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