How to connect to outlook 2010 while its running in c#?

Jason picture Jason · Jun 23, 2011 · Viewed 7.2k times · Source

What I am trying to do is add an "Email To..." button to a winform client that opens a new outlook mail window and attaches a file so the user can forward it. I can get the outlook integration working just fine if outlook is not already running. This is a C# .NET 4.0 winforms app, using the Outlook 14.0 interop library, against Outlook 2010 32 bit running on windows 7 64 bit machine. I have the app already compiled to x86 for other reasons so I doubt its a 32/64 bit issue. Here is my code:

// Connect to outlook and create a new mail item
var app = new Outlook.Application();
var ns = app.GetNamespace("MAPI");
var mailItem = (Outlook.MailItem)ns.Application.CreateItem(Outlook.OlItemType.olMailItem);

// create the mail item and attach the file
mailItem.To = "";
mailItem.Subject = "Emailing: " + Path.GetFileName(_currentFilePath);
mailItem.Attachments.Add(_currentFilePath, Outlook.OlAttachmentType.olEmbeddeditem);

// show the email dialog window
mailItem.Display(true);

If outlook is not running, it works flawlessly. Once its open, I get the following error on the very first line where it tries to create the Outlook.Application object:

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)).

Any ideas what would cause this? Is this a version conflict of some sort?

Answer

Jahmic picture Jahmic · Feb 14, 2012

This is due to privileges of the process. I usually run Visual studio as administrator, but if outlook is not previously started as also admin, the COM call will fail.

Simple solution. Run both as administrator or run both as normal privilege level.