I have code like this
var mi = MyAddIn.Application.ActiveInspector().CurrentItem as MailItem;
if (mi != null)
{
mi.Attachments.Add(myFilePath);
}
The problem with this code is that it assumes that new mail dialog will be opened (the ActiveInspector
CurrentItem
is a MailItem
). However, that is not always so in my case.
If that is not the case, then above code getting mi
will throw NullReferenceException
.
How do I check if I have new mail dialog opened and if so then use above line to get it; otherwise create new MailItem (new mail dialog)?
I am trying to do something like this:
var mi;
if (MyAddIn.Application.ActiveInspector().CurrentItem != null)
{
// get existing
mi = MyAddIn.Application.ActiveInspector().CurrentItem as MailItem;
}
else
{
// otherwise, create new one
mi = MyAddIn.Application.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
}
// now use it to attach file
if (mi != null)
{
mi.Attachments.Add(myFilePath);
}