Troubleshooting Error – Unable to cast COM object {00063034-0000-0000-C000-000000000046}

Have you ever suffered from the following error while working with COM objects?

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

I was a bit clueless why this is occurring even the code is perfect.

Dim fname, email, cname As String
Dim olContactItem As Outlook.ContactItem
Dim ContactsFolder As Outlook.MAPIFolder = Globals.ThisAddIn.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts)

For i As Integer = 1 To ContactsFolder.Items.Count

If TypeOf (ContactsFolder.Items(i)) Is Outlook.ContactItem Then

olContactItem = ContactsFolder.Items(i)
fname = olContactItem.FirstName
email = olContactItem.Email1Address
cname = olContactItem.CompanyName
Marshal.ReleaseComObject(olContactItem)

End If

Next

Later came to know about the fact that the objects are not released by Garbage Collector even if we call it and nullify the object using Nothing keyword. Here are some points that we need to take care while coding with COM objects like ContactItem, MailItem etc.

  • Declare all objects explicitly and release them each pass through the loop using Marshal.ReleaseComObject()
  • Try using a for() loop rather than a foreach() loop, the foreach() is not only slower but also creates hidden instance variables
  • Make sure no compound dot operators are being used. Instantiate an explicit object for each dot operator, that way you can release them explicitly

Hope this helps!

Advertisements
This entry was posted in Troubleshooting and tagged , , , by Pranav. Bookmark the permalink.

About Pranav

A Software Engineer by profession and a Geek at heart. I love exploring new technologies and working on algorithmic problems. As of now, I write code for bread, butter and satisfaction. I like to develop applications, and generally that involves much more than writing code. It’s not just about hurling code, but producing something meaningful that has a measurable and positive impact, helps others and solves interesting problems.

One thought on “Troubleshooting Error – Unable to cast COM object {00063034-0000-0000-C000-000000000046}

  1. Hi Pranav! Thank you soo soo much for this! Worked like a charm, and you helped me out a lot! For the C# users out there, do the following:

    contacts = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);

    for (int i = 1; i < contacts.Items.Count + 1; i++)
    {
    if (contacts.Items[i] is Outlook.ContactItem)
    {

    contact = (Outlook.ContactItem)contacts.Items[i];

    //DO WHAT YOU NEED TO DO WITH THE CONTACT HERE

    Marshal.ReleaseComObject(contact);
    }

    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s