首页 > 解决方案 > Outlook:启动后检测“发送/接收”何时完成

问题描述

我正在用 C# 开发一个 Outlook 插件。

我希望能够在启动后检测到 Outlook 何时完成“发送/接收”选项,以便我可以对收到的邮件项目运行操作。

到目前为止我已经尝试过:

这里有什么解决方案可以可靠地工作?

标签: c#outlookoffice-interop

解决方案


似乎有一个黑客可以检测同步何时完成;即Application.Reminders.BeforeReminderShow按照 DWE 在此 SO 答案中的建议覆盖 此事件(在我的测试中)总是在 Outlook 同步完成后触发。然后,为了确保提醒窗口触发,在启动时添加一个新提醒,然后在 Reminders_BeforeReminderShow 中再次隐藏提醒

然后代码类似于:

public partial class ThisAddIn
{

    private ReminderCollectionEvents_Event reminders; //don't delete, or else the GC will break things

    AppointmentItem hiddenReminder;

    private void ThisAddIn_Startup(object sender, EventArgs e)
    {
            //other stuff
            hiddenReminder = (AppointmentItem)Application.CreateItem(OlItemType.olAppointmentItem); //add a new silent reminder to trigger Reminders_BeforeReminderShow.This method will be triggered after send/receive is finished
            hiddenReminder.Start = DateTime.Now;
            hiddenReminder.Subject = "Autogenerated Outlook Plugin Reminder";
            hiddenReminder.Save();

            reminders = Application.Reminders;
            reminders.BeforeReminderShow += Reminders_BeforeReminderShow;
    }

    private void Reminders_BeforeReminderShow(ref bool Cancel)
    {
        if (hiddenReminder == null) return;

        bool anyVisibleReminders = false;
        for (int i = Application.Reminders.Count; i >= 1; i--)
        {
            if (Application.Reminders[i].Caption == "Autogenerated Outlook Plugin Reminder") //|| Application.Reminders[i].Item == privateReminder
            {
                Application.Reminders[i].Dismiss();
            }
            else
            {
                if (Application.Reminders[i].IsVisible)
                {
                    anyVisibleReminders = true;
                }
            }
        }

        Cancel = !anyVisibleReminders;
        hiddenReminder?.Delete();
        hiddenReminder = null;
            //your custom code here
    }

}

是的,这很笨拙,但这是使用 Outlook 的本质,我还没有看到任何可以声称可靠工作的免费替代方案,而这适用于我尝试过的所有用例。所以这是我愿意为获得可行的解决方案而付出的努力。


推荐阅读