下载电子邮件报告中只有3个用户属性中的1或2个



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

Object reference not set to an instance of an object.

如何绕过此错误并在缺少用户属性的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 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
    }
}

最新更新