【问题标题】:Outlook VSTO - How to check if MailItem exist (new email dialog is open and is active inspector)Outlook VSTO - 如何检查 MailItem 是否存在(新电子邮件对话框已打开并且是活动检查器)
【发布时间】:2016-12-17 12:48:07
【问题描述】:

我有这样的代码

var mi = MyAddIn.Application.ActiveInspector().CurrentItem as MailItem;

if (mi != null)
{
    mi.Attachments.Add(myFilePath);
}

此代码的问题在于它假定将打开新邮件对话框(ActiveInspectorCurrentItemMailItem)。但是,我的情况并非总是如此。

如果不是这样,那么上面获取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);
}

【问题讨论】:

    标签: c# vsto mailitem


    【解决方案1】:

    您需要先检查 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 了解更多信息。

    【讨论】:

    • 非常感谢尤金!
    猜你喜欢
    • 2012-08-30
    • 1970-01-01
    • 2010-12-19
    • 1970-01-01
    • 1970-01-01
    • 2016-02-21
    • 2020-02-27
    • 1970-01-01
    • 2017-01-28
    相关资源
    最近更新 更多