3

I'm trying to do an action on a selected attachment in outlook 2010. I created an Outlook VSTO project in VS2012.

This is the XML for adding a button on the attachment ribbon:

<?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>

This is the code in ThisAddIn.cs

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

This is the ProcessAttachment class:

[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?
        }
    }
}

How can I get the selected attachment here?

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

コンテキストを使用します。この場合は、AttachmentSelection である control.Context からです。

AttachmentSelection ats = (AttachmentSelection)control.Context;

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