Outlook, .NET, and WordML- the SECOND of a short series.

 So, Outlook programming is not always hard, but you eventually end up in the world of MAPI properties if you stay with it. That’s when it gets…uh…fun. Anyway, some things are still easy. Here is an example from my solution (see previous blog entry: https://weblogs.asp.net/johnrdurant/archive/2004/02/27/81075.aspx) that exports my Outlook tasks to a simple XML file.

 

This is some C# code that pulls my tasks out an dumps them to the file. Most of the code is fairly pedestrian in nature, just appending nodes etc. At the end, I check whether my XML output file exists. If it does, I delete it. If I truly had the time to be smart, I would create a simple mechanism for just creating a new file and appending some unique value so I could keep an archive of previous exports. That’s for you to add!

 

Some of this is a little hurried, but you will get the idea.

 

XmlDocument xmlDoc=new XmlDocument();

xmlDoc.AppendChild(xmlDoc.CreateProcessingInstruction("xml", "version=\"1.0\""));

xmlDoc.AppendChild(xmlDoc.CreateProcessingInstruction("mso-solutionextension","URI=\"OutlookTaskExporter\" manifestPath=\"OutlookTaskExporterSolution.xml\""));

XmlElement newRoot = xmlDoc.CreateElement("Tasks","OutlookTaskExporter");

xmlDoc.AppendChild(newRoot);

XmlNode root = newRoot;

XmlNode newNode;

newNode=xmlDoc.CreateElement("SummaryInfo","OutlookTaskExporter");

newNode.InnerText="Status Report for: John R. Durant--" + System.DateTime.Now.ToShortDateString();

root.AppendChild(newNode);

XmlAttribute attNode;

Outlook.Application app=new Outlook.ApplicationClass();

Outlook.NameSpaceNS=app.GetNamespace("MAPI");

Outlook.MAPIFolder tskFld=NS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderTasks);

stringcategories;

foreach(Outlook.TaskItem t in tskFld.Items)

     {

     try

          {

                categories=t.Categories;

                newNode=xmlDoc.CreateElement("Task","OutlookTaskExporter");

                attNode=xmlDoc.CreateAttribute("Subject");

                attNode.InnerText=t.Subject;

                newNode.Attributes.Append(attNode);

                attNode=xmlDoc.CreateAttribute("Due");

                attNode.InnerText=t.DueDate.ToShortDateString();

                newNode.Attributes.Append(attNode);

                attNode=xmlDoc.CreateAttribute("PercentComplete");

                attNode.InnerText=t.PercentComplete.ToString();

                newNode.Attributes.Append(attNode);

                string[] str=categories.Split(new char [] { ',' });

                for(int i=0;i<=str.GetUpperBound(0);i++)

                {

                     XmlNode catNode=xmlDoc.CreateElement("category","OutlookTaskExporter");

                     catNode.InnerText=str[i].TrimStart(new char [] { ' ' });

                     newNode.AppendChild(catNode);

                }

                XmlNode notesNode=xmlDoc.CreateElement("Notes","OutlookTaskExporter");

                notesNode.InnerText=t.Body;

                newNode.AppendChild(notesNode);

                root.AppendChild(newNode);

          }

           catch (System.Exception exc)

           {continue;}

     }

     string filePath = "C:\\MyReports\\myTasks.xml";

     FileInfo outputFile = new FileInfo(filePath);

     if (outputFile.Exists)

     File.Delete(filePath);

 

One of the things to which one should pay particular attention is the area I have highlighted. This processing instruction is absolutely vital for the balance of the solution, so you should not leave it out.

 

One question that you will have is probably, “Why just tasks? Can I not include calendar items etc?” Of course. I focused on tasks, but you could point to other things as well.

 

My next entry will discuss more about the mso-solutionextension processing instruction and what it means, how it hooks up, what it does.

 

Rock thought for this entry: Has anyone listened to the new P.O.D. album? Is it any good? I loved their album Satellite. Several good tunes on there with some overdrive guitar power!

 

Rock on!