luigivampa
luigivampa

Reputation: 397

How to programmatically add a task to a specific Outlook store

I've got a piece of code that can get the tasks of a specific store.

Outlook.Application app = new Outlook.Application();
        Outlook.NameSpace ns = app.Session;
        Outlook.Stores stores = ns.Stores;

        try
        {
            foreach (Outlook.Store store in stores)
            {
                if (store.DisplayName == comboBoxAccounts.Text)
                {
                    Outlook.MAPIFolder tasksFolder = store.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderTasks);

                    foreach (Outlook.TaskItem task in tasksFolder.Items)
                    {
                        if (task.Subject == comboBoxContacts.Text)
                        {
                            comboBoxProperties.Items.Add(task.Body);
                        }
                    }

                    if (tasksFolder != null)
                        Marshal.ReleaseComObject(tasksFolder);
                }
            }
        }
        catch (System.Runtime.InteropServices.COMException ex)
        {
            statusLabel.Text = ex.ToString();
        }
        finally
        {
            if (ns != null)
                Marshal.ReleaseComObject(ns);
        }

I'm trying to put together the equivalent to add a task to a specific store. The code below adds the task to the primary account in Outlook but I want to use another account.

Outlook.Application app = new Outlook.Application();
            Outlook.NameSpace ns = app.Session;

            try
            {
                Outlook.TaskItem task = app.CreateItem(Outlook.OlItemType.olTaskItem);
                task.Subject = strContact;
                task.StartDate = DateTime.Now;
                task.DueDate = DateTime.Now.AddDays(2);
                task.Status = Outlook.OlTaskStatus.olTaskNotStarted;
                task.Body = xDoc.ToString();
                task.Save();

            }
            finally
            {

            }

How do I do something akin to store.CreateItem()?

Thanks

UPDATE: The solution

Outlook.Application app = new Outlook.Application();
            Outlook.NameSpace ns = app.Session;
            Outlook.Stores stores = ns.Stores;

            try
            {
                foreach (Outlook.Store store in stores)
                {
                    if (store.DisplayName == strAccount)
                    {
                        Outlook.MAPIFolder tasksFolder = store.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderTasks);
                        Outlook.TaskItem task = (Outlook.TaskItem)tasksFolder.Items.Add(Outlook.OlItemType.olTaskItem);

                        task.Subject = strContact;
                        task.StartDate = DateTime.Now;
                        task.DueDate = DateTime.Now.AddDays(2);
                        task.Status = Outlook.OlTaskStatus.olTaskNotStarted;
                        task.Body = xDoc.ToString();

                        task.Save();
                    }
                }
            }
            finally
            {

            }

Upvotes: 1

Views: 219

Answers (1)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66276

Don't use Application.CreateItem. Retrieve the store from the Application.Session.Stores collection, call Store.GetDefaultFolder(olFolderTasks), add the item in that folder using MAPIFolder.Items.Add.

Upvotes: 1

Related Questions