How can I get task-specific properties from a MailItem

泄露秘密 提交于 2020-05-15 09:18:21

问题


I've been having a little weekend project for myselff which involves getting all my ToDo tasks from Outlook, put them in a DataGridView and me being able to edit and export them.

The only problem I've been running into is me being unable to get the task specific properties for flagged emails while they still exist, I just don't see any way to access them.

Here is a portion of my current code.

private void retrieveTasks()
    {

        //Clear datagrid so we won't have duplicate information
        taskList.Rows.Clear();

        //Define some properties so we can use these to retrieve the tasks
        Outlook.Application app = null;
        _NameSpace ns = null;
        Store outlookStore = null;
        Outlook.MAPIFolder taskFolder = null;
        Outlook.MAPIFolder specialFolder = null;
        TaskItem task = null;
        DateTime nonDate = new DateTime(4501, 1, 1);

        try
        {
            //Connect to Outlook via MAPI
            app = new Outlook.Application();
            ns = app.GetNamespace("MAPI");
            ns.Logon(null, null, false, false);

            /*
            outlookStore = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox).Store;
            taskFolder = outlookStore.GetSpecialFolder(Microsoft.Office.Interop.Outlook.OlSpecialFolders.olSpecialFolderAllTasks);
            */

            //Get the taskfolder containing the Outlook tasks
            //taskFolder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderTasks);
            outlookStore = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox).Store;
            taskFolder = outlookStore.GetSpecialFolder(OlSpecialFolders.olSpecialFolderAllTasks);
            //Console.WriteLine(specialFolder.Items[1].Subject.ToString());

            /*for (int i = 1; i <= specialFolder.Items.Count; i++)
            {
                //task = (Outlook.TaskItem)specialFolder.Items[i];
                Console.WriteLine(specialFolder.Items[i].ToString());
            }*/

            for (int i = 1; i <= taskFolder.Items.Count; i++)
            {
                //Get task from taskfolder
                Object item = taskFolder.Items[i];
                if (item is Outlook.MailItem)
                {
                    MailItem mail = (Outlook.MailItem)item;
                    if (mail.TaskCompletedDate.Equals(nonDate))
                    {
                        string percentComplete = "";
                        string taskPrio = "";
                        if (mail.UserProperties.Find("Prio") == null)
                        {
                            taskPrio = "";
                        }
                        else
                        {
                            taskPrio = mail.UserProperties.Find("Prio").Value.ToString();
                        }

                        //mail.UserProperties.

                        if (mail.UserProperties.Find("% Complete") == null)
                        {
                            percentComplete = "";
                        }
                        else
                        {
                            percentComplete = mail.UserProperties.Find("% Complete").Value.ToString();
                        }

                        //Add the tasks details to the datagrid
                        taskList.Rows.Add(
                            i.ToString(),
                            checkForNull(mail.Subject),
                            parseDate(mail.TaskStartDate.ToString()),
                            parseDate(mail.TaskDueDate.ToString()),
                            percentComplete, "Taak voltooid",
                            //statusToFriendlyName(mail.Status.ToString()),
                            taskPrio
                        );
                    }
                }
                else if (item is Outlook.TaskItem)
                {
                    task = (Outlook.TaskItem)item;
                    Console.WriteLine(task.Subject);
                    if (task.Complete == false)
                    {
                        string taskPrio = "";

                        //Make sure custom task property is failed or set it to empty text to prevent crashes
                        if (task.UserProperties.Find("Prio") == null)
                        {
                            taskPrio = "";
                        }
                        else
                        {
                            taskPrio = task.UserProperties.Find("Prio").Value.ToString();
                        }

                        //Add the tasks details to the datagrid
                        taskList.Rows.Add(
                            i.ToString(),
                            task.Subject.ToString(),
                            parseDate(task.StartDate.ToString()),
                            parseDate(task.DueDate.ToString()),
                            task.PercentComplete.ToString(),
                            statusToFriendlyName(task.Status.ToString()),
                            taskPrio
                        );
                    }
                }


            }
        }
        catch (System.Runtime.InteropServices.COMException ex)
        {
            MessageBox.Show(ex.ToString());
        }
        finally
        {
            //Release Outlook sources
            ns = null;
            app = null;
        }
    }

So specifically I am looking for the "% complete" property and the status property, everything else I am able to work around on.

It would make my life SO MUCH EASIER if I can get this to work :)

Hope to hear from anyone on here!

Jeffrey


回答1:


Do you mean you need to retrieve the task specific properties from a MailItem object (as opposed to the TaskItem object)?

Take a look at the message with OutlookSpy - click IMessage button, look at the MAPI properties in the GetProps tab. Any property can be accessed using MailItem.PropertyAccessor.GetProperty. The DASL name to be used by GetProperty is displayed by OutlookSpy (select the property in the IMessage window, see the DASL edit box).



来源:https://stackoverflow.com/questions/17521017/how-can-i-get-task-specific-properties-from-a-mailitem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!