1

以下代码可以正常工作以获取 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)).

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

4

1 回答 1

2

OutlookItems 包含组和联系人,而您只对联系人感兴趣,所以像这样获取它们:

  foreach (var item in OutlookItems) {
    var contact = item as ContactItem;
    if (contact != null) {
      Console.WriteLine("FirstName " + contact.FirstName);
    }
  }
于 2013-06-18T10:01:29.867 回答