首页 > 解决方案 > 如何以编程方式将任务添加到特定的 Outlook 存储

问题描述

我有一段代码可以获取特定商店的任务。

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);
        }

我正在尝试将等价物放在一起以将任务添加到特定商店。下面的代码将任务添加到 Outlook 中的主帐户,但我想使用另一个帐户。

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
            {

            }

我该如何做类似于 store.CreateItem() 的事情?

谢谢

更新:解决方案

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
            {

            }

标签: c#outlook

解决方案


不要使用Application.CreateItem. 从Application.Session.Stores集合中检索商店,调用Store.GetDefaultFolder(olFolderTasks),使用 将项目添加到该文件夹​​中MAPIFolder.Items.Add


推荐阅读