C# 下载仅存在3个用户属性中的1个或2个的电子邮件报告

C# 下载仅存在3个用户属性中的1个或2个的电子邮件报告,c#,outlook,vsto,outlook-addin,office-interop,C#,Outlook,Vsto,Outlook Addin,Office Interop,我总共有3个自定义用户属性,但并非所有电子邮件都有这三个属性。有时,电子邮件只能有3个用户属性中的2个。现在的问题是,当我尝试将所有电子邮件下载到Excel中时,我会出现以下错误,因为一些电子邮件缺少这些用户属性 对象引用未设置为对象的实例。 如何绕过此错误并使Excel中缺少用户属性的单元格为空 这是我的密码 private void Export_Click(object sender, RibbonControlEventArgs e) { Out

我总共有3个自定义用户属性,但并非所有电子邮件都有这三个属性。有时,电子邮件只能有3个用户属性中的2个。现在的问题是,当我尝试将所有电子邮件下载到Excel中时,我会出现以下错误,因为一些电子邮件缺少这些用户属性

对象引用未设置为对象的实例。

如何绕过此错误并使Excel中缺少用户属性的单元格为空

这是我的密码

private void Export_Click(object sender, RibbonControlEventArgs e)
        {

            Outlook.UserProperties MailUserProperties = null;
            Outlook.UserProperty MailUserProperty = null;

            Excel.Application oApp = null;
            Excel.Workbook oWB = null;
            Excel.Worksheet oSheet = null;

            oApp = new Excel.Application();
            oWB = oApp.Workbooks.Add();
            oSheet = (Excel.Worksheet)oWB.Worksheets.get_Item(1);


            try
            {
                for (int i = 2; i <= selectedFolder.Items.Count; i++)
                {
                    Outlook.MailItem mail = (Outlook.MailItem)selectedFolder.Items[i];

                    MailUserProperties = mail.UserProperties;

                    oSheet.Cells[i, 1] = i.ToString();
                    oSheet.Cells[i, 2] = mail.Sender;
                    oSheet.Cells[i, 3] = mail.Subject;
                    oSheet.Cells[i, 4] = mail.ReceivedTime.ToLongDateString();

                    for (int j = 1; j <= MailUserProperties.Count; j++)
                    {
                        MailUserProperty = MailUserProperties[j];
                        if (MailUserProperty != null)
                        {
                            try
                            {
                                oSheet.Cells[i, 5] = mail.UserProperties["Ownership"].Value;
                                oSheet.Cells[i, 6] = mail.UserProperties["CompletedTime"].Value;
                                oSheet.Cells[i, 7] = mail.UserProperties["TimeSpent"].Value;
                            }
                            catch(Exception ex)
                            {
                                MessageBox.Show("The following error occured." + ex.Message);
                            }
                        }
                    }
                }
                oSheet.UsedRange.Columns.AutoFit();
            }

            catch (System.Runtime.InteropServices.COMException ex)
            {
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                    // Code to save in Excel
            }
        }
private void导出\u单击(对象发送方,RibbonControlEventArgs e)
{
Outlook.UserProperties MailUserProperties=null;
Outlook.UserProperty MailUserProperty=null;
Excel.Application oApp=null;
Excel.Workbook oWB=null;
Excel.Worksheet oSheet=null;
oApp=新的Excel.Application();
oWB=oApp.Workbooks.Add();
oSheet=(Excel.Worksheet)oWB.Worksheets.get_项(1);
尝试
{
对于(int i=2;i),我将为每个对象创建一个try/catch节。
然后,当其中一个属性丢失时,可以插入零字符串


private void导出\u单击(对象发送方,RibbonControlEventArgs e)
{
Outlook.UserProperties MailUserProperties=null;
Outlook.UserProperty MailUserProperty=null;
Excel.Application oApp=新的Excel.Application();
Excel.Workbook oWB=oApp.Workbooks.Add();
Excel.Worksheet oSheet=(Excel.Worksheet)oWB.Worksheets.get_项(1);
尝试
{

(inti=2;我很完美!这正是我想要的。非常感谢。不用担心,伙计,很高兴我能帮上忙
private void Export_Click(object sender, RibbonControlEventArgs e)
{
    Outlook.UserProperties MailUserProperties = null;
    Outlook.UserProperty MailUserProperty = null;

    Excel.Application oApp = new Excel.Application();
    Excel.Workbook oWB = oApp.Workbooks.Add();
    Excel.Worksheet oSheet = (Excel.Worksheet)oWB.Worksheets.get_Item(1);

    try
    {
        for (int i = 2; i <= selectedFolder.Items.Count; i++)
        {
            Outlook.MailItem mail = (Outlook.MailItem)selectedFolder.Items[i];

            MailUserProperties = mail.UserProperties;

            oSheet.Cells[i, 1] = i.ToString();
            oSheet.Cells[i, 2] = mail.Sender;
            oSheet.Cells[i, 3] = mail.Subject;
            oSheet.Cells[i, 4] = mail.ReceivedTime.ToLongDateString();

            for (int j = 1; j <= MailUserProperties.Count; j++)
            {
                MailUserProperty = MailUserProperties[j];
                if (MailUserProperty != null)
                {
                    var ownership = string.Empty;
                    var completedTime = string.Empty;
                    var timeSpent = string.Empty;

                    try
                    {
                        ownership = mail.UserProperties["Ownership"].Value;
                    }
                    catch (Exception)
                    {
                        ownership = string.Empty; //or you can pass a string like <MISSING>
                    }
                    finally
                    {
                        oSheet.Cells[i, 5] = ownership;
                    }

                    try
                    {
                        completedTime = mail.UserProperties["CompletedTime"].Value;
                    }
                    catch (Exception)
                    {
                        completedTime = string.Empty;
                    }
                    finally
                    {
                        oSheet.Cells[i, 6] = completedTime;
                    }

                    try
                    {
                        timeSpent = mail.UserProperties["TimeSpent"].Value;
                    }
                    catch (Exception)
                    {
                        timeSpent = string.Empty;
                    }
                    finally
                    {
                        oSheet.Cells[i, 7] = timeSpent;
                    }

                }
            }
        }
        oSheet.UsedRange.Columns.AutoFit();
    }

    catch (System.Runtime.InteropServices.COMException ex)
    {
        Console.WriteLine(ex.ToString());
    }
    finally
    {
        // Code to save in Excel
    }
}