Search code examples
c#comoutlookoffice-interopcom-interop

Outlook Interop Exception (E_NOINTERFACE) in Release mode


I'm trying to make a program that would open a new Outlook 2013 message. I've referenced Microsoft.Office.Interop.Outlook 15.0.0.0.

When running in Debug mode everything works fine but crashes in Release mode with Exception:

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: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

Code:

 var _Outlook = new MSOutlook.Application();
 var _MailItem = _Outlook.CreateItem(MSOutlook.OlItemType.olMailItem) as MSOutlook.MailItem;
 var _Recip = _MailItem.Recipients.Add("[email protected]");
 Recip.Type = (int)MSOutlook.OlMailRecipientType.olTo;

 _MailItem.Recipients.ResolveAll();
 _MailItem.Subject = "xxx";

 _MailItem.Display(false);

where MSOutlook = Microsoft.Office.Interop.Outlook namespace.

I am using .NET Framework 4.5 and Outlook 2013.

How can Release mode affect this? Strangely Debug mode works fine..

I'll be grateful for any ideas how to solve it. Thanks!


Solution

  • I solved this issue by enabling "Prefer 32-bit" in Project Settings -> Build -> General.

    It's probably also possible to resolve the recipients object at runtime via dynamic:

    var _Recip = ((dynamic)_MailItem.Recipients).Add("[email protected]");