Jack
Jack

Reputation: 951

Outlook: detect when "Send/Receive" has finished after startup

I am developing an outlook plugin in C#.

I want to be able to detect when outlook has finished the "send/receive" option after startup, so that I can then run operations on the received mail items.

What I have tried so far:

What is a solution here which will work depenably?

Upvotes: 2

Views: 219

Answers (1)

Jack
Jack

Reputation: 951

It seems that there is a hack to detect when syncing has finished; namely to override Application.Reminders.BeforeReminderShow as DWE suggests in this SO answer here This event (in my testing) always fires after outlook syncing has finished. Then, in order to make sure the reminder window fires, you add a new reminder at startup, and then hide the reminder again within Reminders_BeforeReminderShow

The code then being something like:

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
    }

}

Yup, this is very kludgy, but that's the nature of working with outlook, and I haven't seen any free alternative which can actually claim to work reliably, whereas this works in all of the use cases I've tried it in. So that's a hit I'm willing to take to get a working solution.

Upvotes: 2

Related Questions