'Mark Outlook TaskItem as Completed in C#

I'm creating an Outlook Task with status marked as Completed

                    if (task != null)
                    {
                        task.Subject = mi.Subject;
                        task.Status = (Outlook.OlTaskStatus)2; //Status remains 0
                        task.StartDate = DateTime.Now;
                        task.Save();
                    }

Outlook.OlTaskStatus is an enum and value of Complete state is 2. But it remains 0 and task is not marked as completed. Number of ways i've tried till to mark task as completed are as following:

  1. task.Status = Outlook.OlTaskStatus.olTaskComplete;
  2. task.MarkComplete();
  3. task.Complete=true;
  4. task.Status = (Outlook.OlTaskStatus)2;

Nothing appears to be effective and Status is still 0. Can someone please suggest the right way to mark an outlook task Complete ?



Solution 1:[1]

The TaskItem.MarkComplete method marks the task as completed and sets PercentComplete to "100%", Complete to true, and DateCompleted to the current date.

I think you need to change the order in which properties and methods are called:

if (task != null)
{
    task.Subject = mi.Subject;
    task.StartDate = DateTime.Now;
    task.Save();
   
    // the following call should set the status
    task.MarkComplete();
    task.Save();
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Eugene Astafiev