2012-09-13 34 views
6

我目前正在使用C#編寫Outlook 2010 AddIn。我想要的是從AppointmentItem中獲取來自Recipient對象的CompanyName屬性。因此,擁有AppointmentItem的收件人,我想查找每個收件人的CompanyName,這可能是ExchangeUser。C#Outlook從收件人獲取CompanyName屬性

我的代碼是這樣的:

Recipients recipients = appointmentItem.Recipients; 
foreach (Recipient rec in recipients) 
{ 
    resolved = rec.Resolve(); 
    if (resolved) 
    { 
     ContactItem contactItem = rec.AddressEntry.GetContact(); 
     String companyName = contactItem.CompanyName;  
     // ... 
    } 

哪裏contactItem總是空。

做這樣的事情也會導致一個空指針。

ExchangeUser u = rec.AddressEntry.GetExchangeUser(); 
companyName = u.CompanyName; 

我根本找不到公司名稱信息。我知道這些信息確實存在。但是,除了CompanyName之外,還有很多其他屬性也會導致NULL指針。

有人可以給我一個提示嗎?

在此先感謝。

回答

3

請嘗試使用以下代碼。爲我工作。

CODE:

bool resolved; 
     Microsoft.Office.Interop.Outlook.Application olApplication = new Microsoft.Office.Interop.Outlook.Application(); 

     // get nameSpace and logon. 
     Microsoft.Office.Interop.Outlook.NameSpace olNameSpace = olApplication.GetNamespace("mapi"); 
     olNameSpace.Logon("Outlook", "", false, true); 

     // get the Calender items 
     Microsoft.Office.Interop.Outlook.MAPIFolder _olCalender = olNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar); 

     // Get the Items (Appointments) collection from the Calendar folder. 
     Microsoft.Office.Interop.Outlook.Items oItems = _olCalender.Items; 



     foreach (object o in oItems) 
     { 

      if (o is Microsoft.Office.Interop.Outlook.AppointmentItem) 
      { 
       Microsoft.Office.Interop.Outlook.Recipients recipients = ((Microsoft.Office.Interop.Outlook.AppointmentItem)o).Recipients; 
       foreach (Microsoft.Office.Interop.Outlook.Recipient rec in recipients) 
       { 
        resolved = rec.Resolve(); 
        if (resolved) 
        { 
         Microsoft.Office.Interop.Outlook.ContactItem contactItem = rec.AddressEntry.GetContact(); 
         MessageBox.Show(contactItem.CompanyName); 
        } 
       } 

      } 
     } 

希望它應該工作。

1

我還沒有嘗試過,但它可能有所幫助。它看起來像CompanyName是一個ContactItem屬性,所以你需要將收件人轉換爲ContactItem。以下代碼使用收件人的電子郵件地址查找地址簿中的聯繫人。從這裏您可能能夠找到公司名稱:

Outlook.MAPIFolder fldContacts = (Outlook.MAPIFolder)OutlookApp.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts); 
for (int i =1; i <aitem.Recipients.Count+1 ; i++) 
{ 
    Outlook.Recipient r = aitem.Recipients.Item(i); 
    if (!r.Resolved) r.Resolve(); 
    if (r.Resolved) 
    { 
    Outlook.ContactItem ci = (fldContacts.Items.Find("[Email1Address] = '" + r.Address + "'") as Outlook.ContactItem); 
    if (ci != null) 
    { 
     //Now you got the contact deal with it here 
    } 
    } 
}