本文介绍了C#Outlook从收件人获取CompanyName属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在使用C#编写Outlook 2010加载项.我想要的是从我从AppointmentItem中提取的收件人对象获取CompanyName属性.因此,有了一个AppointmentItem的接收者,我想找出每个接收者的CompanyName,它可能是一个ExchangeUser.

I am currently writing an Outlook 2010 AddIn using C#. What I want is to get the CompanyName property from a Recipient object that I pull from an AppointmentItem. So, having the Recipients of an AppointmentItem I want to find out the CompanyName of each Recipient, which might be an ExchangeUser.

我的代码是这样的:

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

其中contactItem始终为null.

Where contactItem is always null.

做这样的事情也会导致空指针.

Doing something like this also results in a null pointer.

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

我根本无法获得CompanyName信息.我知道这些信息确实存在.但是,除了CompanyName之外,还有许多其他属性似乎也会导致NULL指针.

I simply cannot get to the CompanyName information. I know the information does exist. However, also a lot of other attributes, besides CompanyName, seem to result in NULL pointers as well.

有人可以给我提示吗?

谢谢.

推荐答案

尝试以下代码.为我工作.

Try with below code. Working for me.

代码:

 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);
                    }
                }

            }
        }

希望它应该起作用.

这篇关于C#Outlook从收件人获取CompanyName属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 23:18