2013-06-18 48 views
1

下面的代码工作正常获取Outlook联系人得到一个联络小组在C#中的Outlook联系人

Microsoft.Office.Interop.Outlook.Items OutlookItems; 
Microsoft.Office.Interop.Outlook.Application outlookObj = new Microsoft.Office.Interop.Outlook.Application(); 
MAPIFolder Folder_Contacts = (MAPIFolder)outlookObj.Session.GetDefaultFolder(OlDefaultFolders.olFolderContacts); 
OutlookItems = Folder_Contacts.Items; 

foreach (ContactItem contact in OutlookItems) 
{ 
    Console.WriteLine("FirstName " + contact.FirstName); 
} 

,但是当我在Outlook中创建一个组,该组中添加联系人,并运行此代码,这会产生一个错误

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: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)). 

为什么发生这种情况,如何解决呢???

回答

2

的OutlookItems同时包含组和联系人,而你只关心接触,所以让他们这样的:

foreach (var item in OutlookItems) { 
    var contact = item as ContactItem; 
    if (contact != null) { 
     Console.WriteLine("FirstName " + contact.FirstName); 
    } 
    }