ActiveExplorer().Selection returns 以前在 Outlook C# 中选择的邮件
ActiveExplorer().Selection returns previously selected mail in Outlook C#
我有以下代码。当用户点击 Reply 或 Reply 按钮时,它会传递将在 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)
}
消息框显示之前选择的邮件,如何释放之前选择的对象?
谢谢。
首先,无需在功能区按钮的事件处理程序中创建新的 Outlook Application
实例:
Outlook.Application application = new Outlook.Application();
相反,您需要使用 Globals.ThisAddIn.Application
属性 或仅使用提供开箱即用的 Application
属性 的插件 class .
其次,必须在全局范围内声明事件源对象,例如:
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)
我有以下代码。当用户点击 Reply 或 Reply 按钮时,它会传递将在 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)
}
消息框显示之前选择的邮件,如何释放之前选择的对象?
谢谢。
首先,无需在功能区按钮的事件处理程序中创建新的 Outlook Application
实例:
Outlook.Application application = new Outlook.Application();
相反,您需要使用 Globals.ThisAddIn.Application
属性 或仅使用提供开箱即用的 Application
属性 的插件 class .
其次,必须在全局范围内声明事件源对象,例如:
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)