我目前正在使用C#编写Outlook 2010 AddIn.我想要的是从我从AppointmentItem拉出的Recipient对象中获取CompanyName属性.因此,拥有AppointmentItem的收件人我想找出每个收件人的CompanyName,它可能是一个ExchangeUser.
我的代码是这样的:
Recipients recipients = appointmentItem.Recipients;
foreach (Recipient rec in recipients)
{
resolved = rec.Resolve();
if (resolved)
{
ContactItem contactItem = rec.AddressEntry.GetContact();
String companyName = contactItem.CompanyName;
// ...
}
Run Code Online (Sandbox Code Playgroud)
其中contactItem始终为null.
做这样的事也会导致空指针.
ExchangeUser u = rec.AddressEntry.GetExchangeUser();
companyName = u.CompanyName;
Run Code Online (Sandbox Code Playgroud)
我根本无法获取CompanyName信息.我知道这些信息确实存在.但是,除了CompanyName之外,还有许多其他属性似乎也会产生NULL指针.
有人可以给我一个暗示吗?
提前致谢.
尝试使用下面的代码。为我工作。
代码:
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);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
希望它能起作用。