VSTO Outlook:获取选定的附件 [英] VSTO Outlook: Get selected attachment

查看:103
本文介绍了VSTO Outlook:获取选定的附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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>

这是ThisAddIn.cs中的代码

This is the code in ThisAddIn.cs

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

这是ProcessAttachment类:

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?

推荐答案

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

I solved it this way: (this code is just an example and needs improvement)

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:获取选定的附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆