Question

Using Outlook 2010, I get this error when debugging

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

    public void sendEMailThroughOUTLOOK()
    {
        try
        {
            String address = "john.doe@contoso.com";

            Outlook.Application oApp = new Outlook.Application();
            Outlook._MailItem oMailItem = (Outlook._MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem); // CRASHING HERE
            oMailItem.To = address;

            oMailItem.Subject = "Status Update on " + DateTime.Now.ToString("M/d/yyyy");

            oMailItem.BodyFormat = Outlook.OlBodyFormat.olFormatPlain;                

            oMailItem.Body = createEmailBody();
            // body, bcc etc...
            oMailItem.Display(true);
        }//end of try block
        catch (Exception ex)
        {
            Console.WriteLine(ex.InnerException.ToString());
        }//end of catch
    }

This code worked PERFECTLY on Outlook 2013. However it keeps crashing when using Outlook 2010.

I downgraded the Interop.Outlook from 15 to 14

Était-ce utile?

La solution

I think you need to replace this:

Outlook._MailItem oMailItem = (Outlook._MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);

with this:

Outlook.MailItem oMailItem = oApp.CreateItem(Outlook.OlItemType.olMailItem);

Also, if this code is in an add-in, you don't need to create a new instance of Outlook.Application - use the object passed to the OnConnection event.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top