在c#中获取联系人组的Outlook联系人

时间:2013-06-18 09:18:10

标签: c# outlook

以下代码可以很好地获取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)).

为什么会发生这种情况以及如何解决它?

1 个答案:

答案 0 :(得分:2)

OutlookItems包含群组和联系人,您只对联系人感兴趣,所以请按以下方式进行:

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