John R. Durant's WebLog

Blog of "The" Office Developer

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: http://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!

Published Friday, February 27, 2004 6:55 PM by johnrdurant

Comments

 

Enjoy Every Sandwich said:

Take Outs: The Digital Doggy Bag of Blog Bits for 27 February 2004
February 27, 2004 6:17 PM
 

MasterMaq.NET said:

February 27, 2004 7:24 PM
 

Sean Terry said:

Their latest album was a disappointment to me, and I love their Satellite album as well.
February 27, 2004 4:33 PM
 

Simon said:

You might want to suggest a way of getting to the outlook interop assembly to add a reference. I was stuck for 10 mins hunting for where it could possibly be stored on the system as it wasn't registered in the add reference dialog.

I ended up having to copy mine from the gac using cmd and adding a reference to it, set not to copy it locally.

Thanks for the great example!
February 28, 2004 4:17 AM
 

John R. Durant's WebLog said:

May 28, 2004 3:50 PM
 

John R Durant s WebLog Outlook NET and WordML the SECOND of a | Hair Growth Products said:

June 9, 2009 4:22 AM
Anonymous comments are disabled

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker