Unable to cast COM object - Microsoft outlook & C#
Asked Answered
W

5

25

I have written this code to view the unread items in my outlook mail box and here is the code:

 Microsoft.Office.Interop.Outlook.Application app;
 Microsoft.Office.Interop.Outlook.Items items; 
 Microsoft.Office.Interop.Outlook.NameSpace ns; 
 Microsoft.Office.Interop.Outlook.MAPIFolder inbox;

 Microsoft.Office.Interop.Outlook.Application application = new Microsoft.Office.Interop.Outlook.Application();
        app = application;
        ns =  application.Session;
        inbox = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
        items = inbox.Items;
        foreach (Microsoft.Office.Interop.Outlook.MailItem mail in items)
        {
            if (mail.UnRead == true)
            {
                MessageBox.Show(mail.Subject.ToString());
            }
        }

but on the foreach loop I am getting this error:

"Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Outlook.MailItem'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00063034-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE))."

Can you please assist me how to resolve this error?

Windshield answered 11/1, 2011 at 10:0 Comment(4)
@Bolu No, this is what I am writing in my c# Windows applicationWindshield
MAPIFolder is deprecated, use Folder instead.Alkanet
Did you manage to figure this problem out?Maladjusted
This error is consistent with changing Office version. (it creates a whole family of problems) What needs to be done is to edit some registry files as described here: #12958095Backbend
M
30

I had to get around something like your problem a while back.

        foreach (Object _obj in _explorer.CurrentFolder.Items)
        {
            if (_obj is MailItem)
            {
                 MyMailHandler((MailItem)_obj);
            }
        }

Hope that helps.

The issue here is that _explorer.CurrentFolder.Items can contain more objects than just MailItem (PostItem being one of them).

Maladjusted answered 11/1, 2011 at 10:29 Comment(1)
i am getting the same error on following line Microsoft.Office.Interop.Outlook.Application application = new Microsoft.Office.Interop.Outlook.Application();Tyner
H
9

try to check the item is a valid mailitem before checking its properties :

foreach (Object mail in items)
{
    if ((mail as Outlook.MailItem)!=null && (mail as Outlook.MailItem).UnRead == true)
    {
        MessageBox.Show((mail as Outlook.MailItem).Subject.ToString());
    }
}
Hilton answered 11/1, 2011 at 10:30 Comment(0)
N
5

The following code worked fine when I tested it. But I must mention that my reference was to "Microsoft Outlook 14.0 Object Library". Do you happen to use another version?

    public class Outlook
    {
    readonly Microsoft.Office.Interop.Outlook.Items       _items;
    readonly Microsoft.Office.Interop.Outlook.NameSpace   _ns;
    readonly Microsoft.Office.Interop.Outlook.MAPIFolder  _inbox;
    readonly Microsoft.Office.Interop.Outlook.Application _application = new Microsoft.Office.Interop.Outlook.Application(); 

    public Outlook()
    {
        _ns    = _application.Session;
        _inbox = _ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
        _items = _inbox.Items;

        foreach (var item in _items)
        {
            string subject= string.Empty;
            var mail    = item as Microsoft.Office.Interop.Outlook.MailItem;
            if (mail    != null)
                var subject = mail.Subject;
            else
                Debug.WriteLine("Item is not a MailItem");
        }
    }
    }

Please note that in Outlook, many items have some common properties (e.g. expiration time), so you can, as a desperate workaround, use a "dynamic" datatype - either as a fallback scenario for unknown item types or as your default (as long as you're fine with the performance hit).

Noam answered 11/1, 2011 at 10:10 Comment(2)
I tried the same and I got this message: Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Outlook.PostItem'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00063024-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).Windshield
This worked for me with Outlook 2016, with the latest Microsoft.Office.Interop.Outlook.dllAppendicectomy
G
1

Nice! adapted the solution a bit, this worked well for me

foreach (dynamic item in mailItems)
        {
            if (item is MailItem)
            {
                Response.Write("Sender: ");
                Response.Write(item.SenderEmailAddress);
                Response.Write(" - To:");
                Response.Write(item.To);
                Response.Write("<br>");
            }
        }
Galvanometer answered 26/10, 2018 at 18:13 Comment(1)
Are you accessing OOM from a service (IIS)? Don't do that.Phototaxis
M
0

I ran into this problem because I had an email in my outlook folder which was delivery failed email.

Make sure you remove such mails from your folder and try again. For me, removing the email from the folder worked without making any changes in the code.

Metsky answered 8/12, 2023 at 15:5 Comment(1)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewLouisiana

© 2022 - 2024 — McMap. All rights reserved.