【问题标题】:ActiveExplorer().Selection returns previously selected mail in Outlook C#ActiveExplorer().Selection 在 Outlook C# 中返回以前选择的邮件
【发布时间】:2019-08-09 02:06:08
【问题描述】:

我有以下代码。当用户点击回复或回复按钮时,它将传递原始电子邮件,该电子邮件将在 SendAndComplete 按钮上进行处理。

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

现在selectedObject 将在按钮单击时在 Ribbon.cs 上使用。

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

消息框显示之前选择的邮件,如何释放之前选择的对象?

谢谢。

【问题讨论】:

  • “以前选择的”是什么意思?您的意思是说 SelectionChange 事件不会触发,因此您最终会得到一个旧的选择吗?
  • 嗨,Dmitry,是的,SelectionChange 没有启动。由于某种原因,消息框一直显示我第一次编译时选择的第一封电子邮件。 Eugene 的建议帮助我解决了这个问题。

标签: outlook vsto outlook-addin


【解决方案1】:

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

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)

【讨论】:

  • 我还有一个问题,想知道您是否可以提供帮助。我需要保留mailItem,直到用户发送回复。但是,如果在用户之间选择任何其他电子邮件,mailItem 将更改为新的选择,因为currentExplorer.SelectionChange。我该如何解决?谢谢。
  • 你可以创建一个对象列表——例如:List<MailItem>
  • 如果我理解正确,创建List<MailItem> 并添加每个触发SelectionChange 的项目,然后如何检索我要回复的原始项目?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-12
  • 2021-12-21
  • 2015-05-18
  • 2018-11-08
  • 1970-01-01
相关资源
最近更新 更多