VSTO Office Add In c# 回复所有人并不总是 show/fire

VSTO Office Add In c# Reply to all doesn’t always show/fire

我正在为 Outlook 2010 构建一个插件,当用户单击 Reply 时,我希望消息框显示 'Are you sure?'。我们组织中有这么多用户,点击次数太多 Reply 所有人都没有意识到他们在做什么。

这可能看起来有些矫枉过正,但我​​们计划计算收件人的数量,如果它大于 X,就会显示此内容。

我正在使用以下代码,由于某些原因消息框只显示一次,或者每次都会随机显示。

有人能帮忙吗?

private Outlook.Application _application = null;

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    _application = Globals.ThisAddIn.Application;
    _application.ItemLoad += new Outlook.ApplicationEvents_11_ItemLoadEventHandler(_application_ItemLoad);
}

private void GenerateItemMessage(object item, string operation)
{
    MailItem mi = item as Outlook.MailItem;
    MessageBox.Show(String.Format("MailItem {0} will be {1}", mi.Subject, operation));
}

private void ThisAddIn_ReplyAll(object item, ref bool cancel)
{
    GenerateItemMessage(item, "ReplyToAll");
}

private void _application_ItemLoad(object Item)
{
    ((Outlook.ItemEvents_10_Event)Item).ReplyAll += new Outlook.ItemEvents_10_ReplyAllEventHandler(ThisAddIn_ReplyAll);
}

如果要处理事件,必须保持源对象处于活动状态:

private void GenerateItemMessage(object item, string operation)
{
    MailItem mi = item as Outlook.MailItem;
    MessageBox.Show(String.Format("MailItem {0} will be {1}", mi.Subject, operation));
}

mi 对象的范围受声明方法的限制。 GC 可以随时从堆中刷出它。所以,你需要在全局范围内声明它:

MailItem mi = null;
private void GenerateItemMessage(object item, string operation)
{
    mi = item as Outlook.MailItem;
    MessageBox.Show(String.Format("MailItem {0} will be {1}", mi.Subject, operation));
}

或者,如果您要同时处理许多对象,您可以考虑维护一个对象列表。

您应该在代码中的任何地方都这样做,例如,对于以下示例您似乎没有这样做:

private void _application_ItemLoad(object Item)
{
    ((Outlook.ItemEvents_10_Event)Item).ReplyAll += new Outlook.ItemEvents_10_ReplyAllEventHandler(ThisAddIn_ReplyAll);
}

_application_ItemLoad 方法完成后,您将不会触发 ReplyAll 事件。如果你想触发事件,你需要让对象保持活动状态:

Outlook.ItemEvents_10_Event _item;
private void _application_ItemLoad(object Item)
{
    _item = (Outlook.ItemEvents_10_Event)Item;
    _item.ReplyAll += new Outlook.ItemEvents_10_ReplyAllEventHandler(ThisAddIn_ReplyAll);
}

尝试捕获 Explorer.SelectionChange 事件并在所选项目上设置 ReplyAll 事件处理程序,而不是 ItemLoad(或除此之外)。