检查Outlook TaskList项是否已存在

时间:2015-03-20 18:07:03

标签: c# outlook

我想以编程方式从SQL数据库中获取新任务,并将它们添加到我的Outlook任务列表中。查询我需要的所有数据并将它们添加到MS-Outlook的任务列表中是没有问题的。下面是我的代码:

 Outlook.TaskItem oTask = outlookApp.CreateItem(Outlook.OlItemType.olTaskItem);
            oTask.Subject = "Go do task 1";
            oTask.DueDate = Convert.ToDateTime("03/20/2015");
            oTask.StartDate = Convert.ToDateTime("03/20/2015");
            oTask.Body = "test task body";

            oTask.SchedulePlusPriority = "High";
            oTask.Status = Microsoft.Office.Interop.Outlook.OlTaskStatus.olTaskInProgress;

            oTask.Save();

但是,我想检查Outlook任务列表中是否已存在相同的项目(1列或2列的组合)。如果任务项已经存在,则不要保存它。有没有办法做到这一点?

我真的很感激这里的任何帮助。

2 个答案:

答案 0 :(得分:1)

这有用吗? (源代码修改,感谢德米特里)

   private bool IsTaskItemExists(TaskObject oTaskItem)
            {
                Outlook.NameSpace ns = null;
                Outlook.MAPIFolder tasksFolder = null;
                Outlook.Items taskFolderItems = null;
                Outlook.TaskItem task = null;
                Outlook.Application outlookApp = new Application();
                bool foundItem = false;
                try
                {
                    ns = outlookApp.Session;
                    tasksFolder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderTasks);
                    taskFolderItems = tasksFolder.Items;

                    foundItem = (taskFolderItems.Find(String.Format("[Property Name]='{0}'", oTaskItem.Property) ) != null);


                    return foundItem;

                }
                finally
                {
                    if (taskFolderItems != null)
                        Marshal.ReleaseComObject(taskFolderItems);
                    if (tasksFolder != null)
                        Marshal.ReleaseComObject(tasksFolder);
                    if (ns != null)
                        Marshal.ReleaseComObject(ns);
                }
            }

答案 1 :(得分:0)

标准是什么?打开创建任务的文件夹(Application.Session.GetDefaultFolder(olFolderTasks),然后使用Items.Find / FindNext / Restrict

进行搜索
相关问题