ActiveExplorer()。选择返回Outlook C中以前选择的邮件#

ActiveExplorer()。选择返回Outlook C中以前选择的邮件#,outlook,vsto,outlook-addin,Outlook,Vsto,Outlook Addin,我有以下代码。当用户单击“回复”或“回复”按钮时,它将传递原始电子邮件,该电子邮件将在“发送”和“完成”按钮上处理 public partial class ThisAddIn { public object selectedObject = null; Outlook.MailItem mailItem = null; private void ThisAddIn_Startup(object sender, System.EventA

我有以下代码。当用户单击“回复”或“回复”按钮时,它将传递原始电子邮件,该电子邮件将在“发送”和“完成”按钮上处理

public partial class ThisAddIn
    {
        public object selectedObject = null;
        Outlook.MailItem mailItem = null;

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            Outlook.Application application = this.Application;
            Outlook.Explorer currentExplorer = application.ActiveExplorer();

            //Get this event fire when selection changes
            currentExplorer.SelectionChange += new Outlook.ExplorerEvents_10_SelectionChangeEventHandler(CurrentExplorer_Event);
        }

        public void CurrentExplorer_Event()
        {
            if (this.Application.ActiveExplorer().Selection.Count == 1
             && this.Application.ActiveExplorer().Selection[1] is Outlook.MailItem)
            {

                selectedObject = this.Application.ActiveExplorer().Selection[1];
                mailItem = selectedObject as Outlook.MailItem;
                ((Outlook.ItemEvents_10_Event)mailItem).Reply += new Outlook.ItemEvents_10_ReplyEventHandler(MailItem_Reply);
                ((Outlook.ItemEvents_10_Event)mailItem).ReplyAll += new Outlook.ItemEvents_10_ReplyAllEventHandler(MailItem_ReplyAll);
            }
        }

        void MailItem_Reply(object response, ref bool cancel)
        {
            //No code here

        }

        void MailItem_ReplyAll(object response, ref bool cancel)
        {
            //No code here
        }
    }
现在,单击按钮时,将在Ribbon.cs上使用
selectedObject

public void SendnCompleteButton_Click(Office.IRibbonControl control)
        {
            Outlook.Application application = new Outlook.Application();
            var addIn = Globals.ThisAddIn;
            Outlook.MailItem mailItem = addIn.selectedObject as Outlook.MailItem;
            MessageBox.Show(mailItem.Subject + " " + mailItem.ReceivedTime + " " + mailItem.Sender.Name)
        }
消息框显示以前选择的电子邮件,如何释放以前选择的对象


谢谢。

首先,无需在功能区按钮的事件处理程序中创建新的Outlook
应用程序
实例:

Outlook.Application application = new Outlook.Application();
相反,您需要使用
Globals.ThisAddIn.Application
属性,或者只使用提供
Application
属性的外接程序类

其次,必须在全局范围内声明事件源对象,例如:

Outlook.Explorer currentExplorer;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            currentExplorer = Application.ActiveExplorer();

            //Get this event fire when selection changes
            currentExplorer.SelectionChange += new Outlook.ExplorerEvents_10_SelectionChangeEventHandler(CurrentExplorer_Event);
        }
第三,检查Outlook UI中是否选择了单个项目是不正确的。相反,您应该检查是否选择了任何项目:

if (this.Application.ActiveExplorer().Selection.Count > 0)

“先前选择”在什么意义上?您的意思是SelectionChange事件不会启动,因此您会以旧的选择结束吗?您好,Dmitry,是SelectionChange没有启动。由于某些原因,消息框一直显示我第一次编译时选择的第一封电子邮件。尤金的建议帮助我解决了这个问题。我还有另一个问题,我想知道你是否能帮忙。我需要保留
邮件项目
,直到用户发送回复。但是,如果用户选择任何其他电子邮件,
mailItem
将由于
currentExplorer.SelectionChange
而更改为新的选择。我怎样才能修好它?谢谢。您可以创建对象列表-例如:
list
如果我理解正确,请创建
list
并添加每个项目,无论哪个项目触发
SelectionChange
,然后如何检索我回复的原始项目?