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

0

I get the above error, which suspends activity in my app, when run on a particular machine. When I run it on my own machine, no such error occurs.

Perhaps "The RPC server is unavailable" is the crux of the problem, but what would cause that to pop up after the app previously working (and still working on my machine)?

The err msg, in more context (showing what seems to be of value/import), is:

System.InvalidCastException: Unable to cast COM object of type 'Microsoft.Office.Interop.Outlook.ApplicationClass' to interface type 'Microsoft.Office.Interop.Outlook._Application'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00063001-0000-0000-C000-000000000046}' failed due to the following error: The RPC server is unavailable. (Exception from HRESULT: 0x800706BA). at System.StubHelpers.StubHelpers.GetCOMIPFromRCW(Object objSrc, IntPtr pCPCMD, IntPtr& ppTarget, Boolean& pfNeedsRelease) at Microsoft.Office.Interop.Outlook.ApplicationClass.CreateItem(OlItemType ItemType) at RoboReporter2017.ExceptionLoggingService.EmailMessageToAssignee(String unit, String notificationRecipient, String rptName) at RoboReporter2017.RoboRprtrLib.GenerateAndSaveDueReports() at RoboReporter2017.FormMain.RunDueReports() at RoboReporter2017.FormMain.FormMain_Load(Object sender, EventArgs e) . . .

************** Loaded Assemblies ************** ---------------------------------------- Microsoft.Office.Interop.Outlook Assembly Version: 12.0.0.0 Win32 Version: 12.0.4518.1014 CodeBase: file:///C:/Windows/assembly/GAC/Microsoft.Office.Interop.Outlook/12.0.0.0__71e9bce111e9429c/Microsoft.Office.Interop.Outlook.dll ---------------------------------------- office Assembly Version: 12.0.0.0 Win32 Version: 12.0.4518.1014 CodeBase: file:///C:/Windows/assembly/GAC/office/12.0.0.0__71e9bce111e9429c/office.dll ----------------------------------------

The method that's breaking on that machine, as referenced in the err msg, is:

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

I notice that the Microsoft.Office.Interop.Outlook version in my project's References is 12.0.0.0, the same as the one listed among the "Loaded Assemblies" listed in the err msg.

UPDATE

Thinking perhaps that Outlook not running was the problem, I wrote this code:

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

...adapted from here, but before implementing it, I shut down Outlook and ran the app, to see if I would get that same err msg on my machine if Outlook was not running. But no! It restarts Outlook by itself, without the need for my fancy-pants StartOutlookIfNotRunning() method.

So that's not the problem, anyway...

c#
office-interop
assemblies
com-interop
comobject
asked on Stack Overflow Feb 8, 2017 by B. Clay Shannon • edited Feb 8, 2017 by B. Clay Shannon

2 Answers

2

See 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" .

Where and when do you try to automate Outlook?

Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution. Read more about that in the Considerations for server-side Automation of Office article.

answered on Stack Overflow Feb 20, 2017 by Eugene Astafiev
1

Well, although Eugene Astafiev advice is very sound, in twice times that I collided with the ominous

System.InvalidCastException: Unable to cast COM object of type 'Microsoft.Office.Interop.Outlook.ApplicationClass' to interface type 'Microsoft.Office.Interop.Outlook._Application'.

I solved it with Eugene Astafiev's advice in another forum:

regtlib msoutl.olb

from elevated command prompt inside Office App folder.

The catch, for me, was correctly working in some machines and not working in another.

answered on Stack Overflow Nov 15, 2020 by Marcelo Scofano Diniz • edited Nov 16, 2020 by Marcelo Scofano Diniz

User contributions licensed under CC BY-SA 3.0