我总共有3个自定义用户属性,但并非所有电子邮件都具有这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
}
}
谢谢。
答案 0 :(得分:1)
然后,当缺少其中一个属性时,可以插入零字符串。
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
}
}