none
Outlook 2007 contacts RRS feed

  • Question

  • Hi,

    On Outlook 2007 C# code like this:

    OutLook.MAPIFolder cContacts = oNS.GetDefaultFolder(OutLook.OlDefaultFolders.olFolderContacts);
    OutLook.Items oItems = cContacts.Items;

    foreach (OutLook.ContactItem oCt in oItems)

    returns the following exception (at the foreach line):

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

    So i've changed the

    foreach (OutLook.ContactItem oCt in oItems)

    to the following

    foreach (object objOct in oItems)
    {
         var oCt = objOct as OutLook.ContactItem;
         if (oCt == null)
             continue;

    It works but valid items (!= null) are only 7372 over 7534 (oItems.Count)
    Outlook 2007 reports that there are 7518 contacts.

    Is there a way to get the missing ones ?

    Thank you
    Manuel

    Tuesday, June 26, 2012 1:17 PM

Answers

  • If the Class property is OlObjectClass.olDistributionList you can cast the item to a DistListItem.
     
    Don't use foreach() loops with COM objects, you're creating objects that you can't release explicitly.
     
    Probably the easiest way would be something like this:
     
    Outlook.ContactItem contact = null;
    Outlook.DistListItem dist = null;
     
    for (int i = 1; i <= oItems.Count; i++)
    {
        contact = oItems[i] as Outlook.ContactItem;
        if (contact == null)
        {
            dist = oItems[i] as Outlook.DistListItem;

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "Manuel Marini" <=?utf-8?B?TWFudWVsIE1hcmluaQ==?=> wrote in message news:925cb51e-7fb9-442f-847f-699f22d210f0...

    objOct.GetType() returns _ComObject

    In Outlook i've found now that skipped contacts are distribution lists ;)

    How can i cast it ?

    thank you !





    Ken Slovak MVP - Outlook
    • Marked as answer by Manuel Marini Tuesday, June 26, 2012 9:29 PM
    Tuesday, June 26, 2012 3:40 PM

All replies

  • maybe not all items in Contacts folder are of type ContactItem? try printing out for null casts their Class and MessageClass properties and see what they contain.
    Tuesday, June 26, 2012 1:27 PM
  • objOct.GetType() returns _ComObject

    In Outlook i've found now that skipped contacts are distribution lists ;)

    How can i cast it ?

    thank you !




    Tuesday, June 26, 2012 3:05 PM
  • If the Class property is OlObjectClass.olDistributionList you can cast the item to a DistListItem.
     
    Don't use foreach() loops with COM objects, you're creating objects that you can't release explicitly.
     
    Probably the easiest way would be something like this:
     
    Outlook.ContactItem contact = null;
    Outlook.DistListItem dist = null;
     
    for (int i = 1; i <= oItems.Count; i++)
    {
        contact = oItems[i] as Outlook.ContactItem;
        if (contact == null)
        {
            dist = oItems[i] as Outlook.DistListItem;

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "Manuel Marini" <=?utf-8?B?TWFudWVsIE1hcmluaQ==?=> wrote in message news:925cb51e-7fb9-442f-847f-699f22d210f0...

    objOct.GetType() returns _ComObject

    In Outlook i've found now that skipped contacts are distribution lists ;)

    How can i cast it ?

    thank you !





    Ken Slovak MVP - Outlook
    • Marked as answer by Manuel Marini Tuesday, June 26, 2012 9:29 PM
    Tuesday, June 26, 2012 3:40 PM