Home > OS >  Create task in specific mailbox from Outlook vsto add-in
Create task in specific mailbox from Outlook vsto add-in

Time:10-08

I'm trying to create a task using Outlook vsto add-in. The tasks are being created in default folder but I want to save it to another mailbox folder. From this post I understood how I can fetch the default folder but I'm not getting what attribute to use to set the folder path.

  Outlook.TaskItem t = this.Application.CreateItem(Outlook.OlItemType.olTaskItem) as Outlook.TaskItem;
     if (t != null)
       {
           t.Subject = "Hello, Reader";
           // what attribute to refer (t.) to set the folder path
           t.Save();
       }

CodePudding user response:

There are several ways to get the job done:

  1. You can use the CreateItem method and then call the Move one to move the newly created to the required folder:
using System.Runtime.InteropServices;
// ...
private void CreateTaskItemUsingCreateItem(Outlook._Application Application)
{
    Outlook.TaskItem task = Application.CreateItem(Outlook.OlItemType.olTaskItem)
            as Outlook.TaskItem;
    if (task != null)
    {
        task.Subject = "change oil in the car";
        task.Save();
        //task.Display(false);
     
        task.Move(folder);

        Marshal.ReleaseComObject(task);
    }
}
  1. You may consider another way to create a task item by using the Add method of the Items class. This method also accepts the OlItemType enumeration and returns a newly created Outlook item.
using System.Runtime.InteropServices;
// ...
private void CreateTaskItemUsingItemAdd(Outlook._Application Application)
{
    Outlook.NameSpace ns = null;
    Outlook.MAPIFolder taskFolder = null;
    Outlook.Items items = null;
    Outlook.TaskItem task = null;
    try
    {
        ns = Application.GetNamespace("MAPI");
        taskFolder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderTasks);
        items = taskFolder.Items;
        task = items.Add(Outlook.OlItemType.olTaskItem) as Outlook.TaskItem;
        task.Subject = "change oil in the car";
        task.Save();
        task.Display(false);
    }
    catch (Exception ex)
    {
        System.Windows.Forms.MessageBox.Show(ex.Message);
    }
    finally
    {
        if (task != null) Marshal.ReleaseComObject(task);
        if (items != null) Marshal.ReleaseComObject(items);
        if (taskFolder != null) Marshal.ReleaseComObject(taskFolder);
        if (ns != null) Marshal.ReleaseComObject(ns);
    }
}

So, basically, in any Outlook folder you can get an instance of the Items class and use the Add method to create a new instance of the required item type.

See How To: Create a new Task item in Outlook for more information.

CodePudding user response:

Instead of using Application.CreateItem, open the target MAPIFolder object and call MAPIFolder.Items.Add

  • Related