是否获得我单击并拖放的附件?

时间:2018-10-12 19:49:09

标签: c# .net outlook email-attachments

使用Outlook Interop,我不了解如何获取(C#)我单击的附件,然后将其拖动以便拖放。

资源管理器的“ AttachmentSelection”属性返回选定的附件,但未单击。

例如,我选择了“ demo.xlsm”附件,然后单击demo.rar(无需离开鼠标)将其拖到某些winform应用程序中。因此,demo.rar实际上不是选中的(但是demo .xlsm可以。)

我的问题是如何获得被点击的附件?

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码选择附件:

 public void DoSomething(Office.IRibbonControl control)
{
    var window = plugin.Application.ActiveWindow();
    var attachsel = window.AttachmentSelection();

    int? index = null;
    if (attachsel.count > 0)
    {
        var attachment = attachsel[1];
        index = attachment.Index;
    }

    var explorer = plugin.Application.ActiveExplorer();
    var selection = explorer.Selection;

    if ((selection.Count > 0) && (index != null))   
    {
        object selectedItem = selection[1];
        var mailItem = selectedItem as Outlook.MailItem;
        foreach (Outlook.Attachment attach in mailItem.Attachments)
        {
            if (attach.Index == index)
            {
                attach.SaveAsFile(Path.Combine(@"c:\temp\", attach.FileName));
            }
        }

    }
}

有关更多信息,您可以参考以下链接:

VSTO Outlook: Get selected attachment