Outlook VSTO - 如何检查 MailItem 是否存在(新电子邮件对话框已打开并且是活动检查器)
Outlook VSTO - How to check if MailItem exist (new email dialog is open and is active inspector)
我有这样的代码
var mi = MyAddIn.Application.ActiveInspector().CurrentItem as MailItem;
if (mi != null)
{
mi.Attachments.Add(myFilePath);
}
此代码的问题在于它假定将打开新邮件对话框(ActiveInspector
CurrentItem
是 MailItem
)。然而,我的情况并非总是如此。
如果不是这种情况,那么上面获取 mi
的代码将抛出 NullReferenceException
.
如何检查我是否打开了新邮件对话框,如果是,则使用上面的行来获取它;否则创建新的 MailItem(新邮件对话框)?
我正在尝试做这样的事情:
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);
}
您需要先检查 ActiveInspector 方法调用返回的内容。
var mi;
var inspector = MyAddIn.Application.ActiveInspector();
if (inspector != null)
{
// get existing
mi = inspector.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);
}
有关详细信息,请参阅 How to: Programmatically Determine the Current Outlook Item。
我有这样的代码
var mi = MyAddIn.Application.ActiveInspector().CurrentItem as MailItem;
if (mi != null)
{
mi.Attachments.Add(myFilePath);
}
此代码的问题在于它假定将打开新邮件对话框(ActiveInspector
CurrentItem
是 MailItem
)。然而,我的情况并非总是如此。
如果不是这种情况,那么上面获取 mi
的代码将抛出 NullReferenceException
.
如何检查我是否打开了新邮件对话框,如果是,则使用上面的行来获取它;否则创建新的 MailItem(新邮件对话框)?
我正在尝试做这样的事情:
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);
}
您需要先检查 ActiveInspector 方法调用返回的内容。
var mi;
var inspector = MyAddIn.Application.ActiveInspector();
if (inspector != null)
{
// get existing
mi = inspector.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);
}
有关详细信息,请参阅 How to: Programmatically Determine the Current Outlook Item。