Maria Maria - 1 year ago 115
C# Question

Outlook task request accepted event

I'm working on an addon for Outlook and I'm fairly new to this.


I need to know when my task request has been accepted, but I can't find such event.


I tried subscribing to PropertyChange event of the task and check if ResponseState is olTaskAccept, but that doesn't work, maybe because the task object on which this property changes is different than the task object which is listening for the event.


I see there's a TaskRequestAcceptItem object, but msdn doesn't bother to say how to obtain it, only that you can't create it.


So my question is how do I subscribe to an event which fires when the task request I sent is accepted, or at least how can I obtain the DateTime at which it was accepted?


I'm working with VS 2013 and Outlook 2010



void buttonGroup1_Click(Office.CommandBarButton Ctrl, ref bool CancelDefault)
{
_task = Application.CreateItem(Outlook.OlItemType.olTaskItem);
Outlook.MailItem email = _selection[1] as Outlook.MailItem;

_task.Subject = email.Subject;
_task.Body = email.Body;
_task.ReminderSet = true;
_task.ReminderTime = new DateTime(2015, 03, 12, 15, 41, 0);
_task.PropertyChange += task_PropertyChange;
_task.Recipients.Add("[email protected]");
_task.Assign();
(_task as Outlook._TaskItem).Send();
}

Dictionary<Outlook.TaskItem, DateTime> taskAccepts = new Dictionary<Outlook.TaskItem, DateTime>();

void task_PropertyChange(string Name)
{
if (Name == "ResponseState" && _task.ResponseState == Outlook.OlTaskResponse.olTaskAccept)
{
taskAccepts.Add(_task, DateTime.Now); // this never executes
}
}

Answer Source

If anyone else struggles with this here's how I did it. I listen for when a TaskRequestAcceptItem has been added to the inbox :

    Outlook.Items _items;
    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        Outlook.Folder inbox = Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox) as Outlook.Folder;

        _items = inbox.Items;
        _items.ItemAdd += items_ItemAdd;
    }

    private void items_ItemAdd(object Item)
    {
        if (Item is Outlook.TaskRequestAcceptItem)
        {
            Outlook.TaskItem task = (Item as Outlook.TaskRequestItem).GetAssociatedTask(false);
            // some code here
        }
    }

There is another way, if the code runs on the recipient's machine, you could listen for the TaskRequestItem and then listen for the PropertyChange event on the associated task

    Outlook.TaskItem _task;
    private void items_ItemAdd(object Item)
    {
        if (Item is Outlook.TaskRequestItem)
        {
            _task = (Item as Outlook.TaskRequestItem).GetAssociatedTask(false);
            _task.PropertyChange += item_PropertyChange;
        }
    }

    void item_PropertyChange(string Name)
    {
        if (Name == "ResponseState" && _task.ResponseState == Outlook.OlTaskResponse.olTaskAccept)
        {
            //some code here
        }
    }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download