Why am I receiving exception from Office's Outlook library? - c#

I have an application that calls
Email hello = new Email(appropriate constructor);
hello.Email_Send();
I'm receiving the exception:
Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 80080005 Server execution failed (Exception from HRESULT: 0x80080005 (CO_E_SERVER_EXEC_FAILURE)).
from System.Runtime.InteropServices.COMException.
using O = Microsoft.Office.Interop.Outlook;
class Email
{
public void Email_Send()
{
O.Application outlook = new O.Application(); //Exception thrown here.
O.MailItem message = outlook.CreateItem(O.OlItemType.olMailItem);
message.To = Receiver;
message.CC = Sender;
message.Subject = Subject;
message.Body = "This is an automated message sent at " + DateTime.Now.ToString("HH:mm:ss") + " about " + Body_Topic + System.Environment.NewLine + Body_Content ;
message.Send();
}
}
This error has never happened previously, and there has been no change to the code that I know of. http://support.microsoft.com/kb/825118 doesn't seem to fit the my symptoms - My computer doesn't stop responding, etc.
Any help diagnosing the issue would be greatly appreciated!

This error was caused by visual studio being run as admin. Outlook doesn't allow separate users to access the same mailbox (I had the outlook application open on desktop). Even though I have local admin access w/ my domain user, running VS as admin must associate the process to a different user object? Not exactly sure how this works, but... Resolved.

I ran into the same issue, and as previously said: if Visual Studio is running as Administrator then Outlook prevents another instance with a different user. My VS solution is starting several projects, and I need it to run as Administrator, so what I did is run Outlook as administrator while debugging. This solved my problem.

Posting one of the comments as an answer.
When we close the running instance of Outlook it works like a charm.
In my situation I'm running under VMs with special security software. On my developer desktop it worked fine when Outlook was open.
But in my secured VM I had to close outlook in order to get this to work. This is likely to due our security software.
When outlook is left running: Unhandled Exception: System.Runtime.InteropServices.COMException: Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 80080005 Server execution failed (Exception from HRESULT: 0x80080005 (CO_E_SERVER_EXEC_FAILURE)).
When it is not running, it works fine.

The accepted answer is not entirely accurate. Actually, the COM will fail if the calling application does NOT have the same access level as the original process. Both need to be at the same access level.
So, if Outlook was started as Administrator and the program being debugged in Visual Studio is also administrator, it will work.
Vica-versa: if Outlook was NOT started as Administrator, then the program being debugged in Visual Studio will also need to be NON-administrator.
Also, you can get this message if not also included a COMReference to Microsoft.Office.Core.

Related

InternetExplorer COMException: System.Runtime.InteropServices.COMexception: The RPC server is unavailable

I'm very new to C# so please excuse me if it's a silly question.
I created a console application that uses the "InternetExplorer" from SHDocVw. This application goes to a website and performs some operations. It works just fine on my computer, but when I try to publish it I just can't to get it to work on my colleagues computers. These other computers don't have dotnet core installed.
So if I publish the app as framework-dependant I get a hostfxr.dll missing library error and it obviously doesn't work at all.
I thought I could fix this by publishing the app as self-dependant. This didn't help either. I always need to paste the "Interop.MSHTML.dll" and "Interop.SHDocVw.dll" manually. When I do, the app at least starts. Internet explorer shows up, the "Navigate" command works. But as soon as I try to hide the window, or try to work with elements in the page, it just crashes.
There are two types of errors I get:
Unhandled Exception: System.Runtime.InteropServices.COMexception: The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
at SHDocVw.IWebBrowser2.set_Visible(Boolean pBool)
at MyWebApp.IEDriver.Visibility(Boolean isVisible)
at MyWebApp.Program.Main(String[] args)
Other times I get a very similar error, but it's even mentions my own user files, even when running on a different machine. Is that normal?
Unhandled exception. System.Runtime.InteropServices.COMException (0x800706BA): The RPC server is unavailable. (0x800706BA)
at SHDocVw.InternetExplorerClass.get_ReadyState()
at IEAutomation.IEDriver.WaitForComplete() in C:\Users\St3ve\source\repos\MyWebApp\MyWebApp\IEDriver.cs:line 552
at TestWeb.Program.Main(String[] args) in C:\Users\St3ve\source\repos\MyWebApp\MyWebApp\Program.cs:line 82
I tried to to google the errors, but can't get it work, I would be really grateful for any hints or help.
The crux of this approach is to make sure we are accessing the right InternetExplorer object that is associated with our process
private InternetExplorer _IE;
public Process m_Proc = Process.Start(#"C:\Program Files\Internet Explorer\iexplore.exe", "-nomerge www.google.com");
public IEDriver()
{
Thread.Sleep(5000);
_IE = null;
ShellWindows m_IEFoundBrowsers = new ShellWindows();
foreach (InternetExplorer Browser in m_IEFoundBrowsers)
{
if (Browser.HWND == (int)m_Proc.MainWindowHandle)
{
_IE = Browser;
break;
}
}
Then in your methods like WaitForCompleteNew(), Navigate(), etc where ever you attempt to access InternetExplorer's proprties or methods, you can use object _IE.
_IE.Visible = false;
document = ((HTMLDocument)_IE.Document)

COM exception occurs only when I'm logged in to company domain

I have been working on a c# project in VS2017 Community. It's been fine for 6-8 weeks. I went away for the weekend and came back and I'm now getting an exception in my code that was not there previously.
System.Runtime.InteropServices.COMException: 'Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))'
At work I am logged into our company domain with Admin privileges. I was also working on this at home on my laptop to I tried it there and it's fine. Copied the project from laptop back to my desktop in the office and again get the exception.
Finally tried logging out of the computer and logging back in as a local user, and the code runs fine, no exception.
When logging back into the domain I tried running VS2017 'as Administrator' but same problem - exception get thrown. I removed all the most recent Windows Updates but no relief.
I really have to be logged in to the domain in order to do most of my other work, so I'm fairly at a standstill right now.
The purpose of the offending code is very basic - just creating a playlist (from a folder full of files) The code here's the code I'm executing that throws the exception when I'm logged in as a Domain account:
WMPLib.IWMPPlaylist playlist = wmp.playlistCollection.newPlaylist("myplaylist");

C# Get running Outlook instance in VSTO add-in

I am trying to get an Outlook Application object in my add-in for Excel.
If there's a running Outlook instance, it should get that, if there isn't any, it should create one, using the Outlook object model.
This is the code I have right now:
public static Outlook.Application GetApplicationObject()
{
Outlook.Application application = null;
if (Process.GetProcessesByName("OUTLOOK").Count() > 0)
{
application = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;
}
else
{
application = new Outlook.Application();
}
return application;
}
My problem: it finds Outlook processes, but can't get them, throwing the following error message:
Operation unavailable (Exception from HRESULT: 0x800401E3 (MK_E_UNAVAILABLE))
I tried debugging it step by step, and monitored the task manager. I could see that I have an Outlook instance, but it's only an icon in the right side of the taskbar. Does this mean, that the instance is not fully loaded yet, and it can't be accessed, to get the Application object from it?
I ended up modifying my code, and separating the if-else into 2 try-catches, with their own returns, but I still think that the code above should be usable.
Outlook is a singleton, so new Outlook.Application() will always work - if it is already running, you will get that running object.
Make sure both apps (Excel and Outlook) are running in the same security context. Is either app running with elevated privileges (Run As Administrator)?

Autodesk.Inventor.Interop.dll on WinServer 64-bit 2012 R2 - interface non registred

i'm trying to develop a very simple web application based on the Autodesk Inventor engine.
I'm developing on Win7 64-bit with Visual Studio 2010 and Inventor 2015 and everything it's working perfectly on debugging but when i publish on the web server i get the error:
HRESULT: 0x800401F3 (CO_E_CLASSSTRING)
and the message
interface string not valid
The C# code line where i receive the error is:
Inventor.Application _invApp = (Inventor.Application)Marshal.GetActiveObject("Inventor.Application");
The full code for my test is the following:
using Inventor;
using System.Runtime.InteropServices;
namespace web_debug_cs
{
public partial class debug_runinventor : Ssytem.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e) { startInventorApplication(); }
private void startInventorApplication()
{
string sDebug = string.Empty;
try
{
Inventor.Application _invetorApp = (Inventor.Application)Marshal.GetActiveObject("Inventor.Application");
sDebug = "Success!!!";
}
catch (Exception ex) { sDebug = "UNSUCCESS!<br />" + ex.Message; }
lblAnswer.Text = sDebug;
}
}
}
I get this code directly from the Inventor 2015 guide, but (repeat) on the local machine everything it's ok, but not on the server.
I checked the permission (everyone: full control)
I registred manually with regsvr32 (impossible to register) both on system32 and SysWOW64
I registred on the framework 32 and 64 with regasm (registration success!)
I set the web site to work with 32-bit application
but nothing could solve this issue.
I thought about the possibility to import manually the dll with pInvoke, but with no success...
I googled a lot, i tryed to ask to Autodesk with no success.
I started Inventor on the Server to verify if it works, and it work perfectly!
Could anyone try to help me?
Thanks in advance for any kind reply!
Emanuele
I would not expect this to work due a simple reason: the webpage is running as a service, and Inventor runs as a user-level application. Imagine the following: the web page is requested by 100 clients/browsers, your app will launch Inventor 100 times. But more than that, the webpage is not on the user-level.
The best chance to work would be: your webpage receives a request (let's say create a .ipt file), create a record that the action must be performed (for instance, a new .txt file with instructions or a database entry), a Windows services detects the request and launch Inventor to process it. In this case you'll have a queue where requests are placed by your webpage, but processed later.
But I must say this is not supported nor allowed on the EULA, please review it.

Problems accessing Outlook from c# Application

I'm writing a console application, which checks the contents of an outlook mailbox, in order to read the contents of specific emails into a database.
This application works fine within Visual studio, whether or not Outlook is open.
If I build the application and run it from the exe it only works when Outlook is open, which isn't really a problem.
However, I need to run it from a scheduled task as it has to run every few minutes. This will not work at all.
I'm using the following code:
System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName("OUTLOOK");
int collCount = processes.Length;
if (collCount != 0)
{
OutlookApp = Marshal.GetActiveObject("Outlook.Application") as Application;
}
else
{
OutlookApp = new Application();
}
The error message I'm getting is:
System.Runtime.InteropServices.COMException (0x800401E3): Operation
unavailable (Exception from HRESULT: 0x800401E3 (MK_E_UNAVAILABLE))
at System.Runtime.InteropServices.Marshal.GetActiveObject(Guid&
rclsid, IntPtr reserved, Object& ppunk) at
System.Runtime.InteropServices.Marshal.GetActiveObject(String progID)
at ImportCruiseEmails.Program.Main()
On the line :
Marshal.GetActiveObject("Outlook.Application") as Application;
Both Outlook and the console application are running under my user account, which has administrator permissions. I've been pulling my hair out with this all afternoon. Can anybody please shed any light on it? Cheers!
Even through the user accounts are the same, security contexts are different since the scheduler runs as a service. And no Office app can be used in a service.
Your options are
In case of an Exchange Server, use EWS to access the mailbox.
Extended MAPI (C++ or Delphi only)
Redemption (I am its author, any language) - it wraps Extended MAPI and its RDO family of objects can be used from a service.

Categories