简体   繁体   English

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

[英]getting a outlook contact of a contact group in c#

Following code works fine to get outlook contact 以下代码可以很好地与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);
 }

but when i create a group in outlook and add contact in that group and run this code ,this generates an error 但是当我在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)).

why this is happening and how to solve it??? 为什么会发生这种情况以及如何解决呢???

The OutlookItems contains both group and contacts, and you're interested only in contacts, so get them like this: OutlookItems包含组和联系人,并且您仅对联系人感兴趣,因此请按以下方式获取它们:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM