Outlook Interop Exception (E_NOINTERFACE) in Release mode

-1

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("xxx@yyy.com");
 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!

c#
com
outlook
office-interop
com-interop
asked on Stack Overflow Nov 2, 2015 by dontbyteme • edited Nov 14, 2017 by dontbyteme

3 Answers

0

Try to explicitly declare the _Outlook variable type:

MSOutlook.Application _Outlook = new MSOutlook.Application();
0

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("xxx@yyy.com");
answered on Stack Overflow Nov 14, 2017 by dontbyteme
-2

Where and when do you try to run the code? Did you have a chance to check out the list of running processes? Does it contain the OUtlook.exe entry?

Try to use Reflection to create a new Application instance:

Activator.CreateInstance(Type.GetTypeFromProgID("Outlook.Application"));
answered on Stack Overflow Nov 2, 2015 by Eugene Astafiev

User contributions licensed under CC BY-SA 3.0