3

我正在尝试对 Outlook 2010 中的选定附件执行操作。我在 VS2012 中创建了一个 Outlook VSTO 项目。

这是用于在附件功能区上添加按钮的 XML:

<?xml version="1.0" encoding="UTF-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">
  <ribbon>
    <contextualTabs>
      <tabSet idMso="TabSetAttachments">
        <tab idMso="TabAttachments">
          <group label="MyGroup" id="MyAttachmentGroup">
            <button id="AttachButton"
                size="large"
                label="Do something"
                imageMso="HappyFace"
                onAction="DoSomething" />
          </group>
        </tab>
      </tabSet>
    </contextualTabs>
  </ribbon>
</customUI>

这是 ThisAddIn.cs 中的代码

protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
{
    return new ProcessAttachment(this);
}

这是 ProcessAttachment 类:

[ComVisible(true)]
public class ProcessAttachment : Office.IRibbonExtensibility
{
    private Office.IRibbonUI ribbon;
    private ThisAddIn plugin;

    public ProcessAttachment(ThisAddIn plugin)
    {
        this.plugin = plugin;
    }

    public void Ribbon_Load(Office.IRibbonUI ribbonUI)
    {
        this.ribbon = ribbonUI;
    }

    public void DoSomething(Office.IRibbonControl control)
    {
        var explorer = plugin.Application.ActiveExplorer();
        var selection = explorer.Selection;

        if (selection.Count > 0)   
        {
            object selectedItem = selection[1];
            var mailItem = selectedItem as Outlook.MailItem;
            //How to get selected attachment?
        }
    }
}

如何在此处获取选定的附件?

4

2 回答 2

7

我是这样解决的:(这段代码只是一个例子,需要改进)

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));
            }
        }

    }
}
于 2013-03-05T08:35:52.457 回答
2

https://msdn.microsoft.com/en-us/library/office/ee692172(v=office.14).aspx#OfficeOLExtendingUI_AttachmentContextMenu

使用上下文,即。来自 control.Context 的 AttachmentSelection 就是这种情况。

AttachmentSelection ats = (AttachmentSelection)control.Context;

ats[1] <- your selected attachment
于 2015-08-10T12:33:06.723 回答