Click here to Skip to main content
15,867,453 members
Articles / Desktop Programming / MFC
Article

.NET Connector for Microsoft Outlook

Rate me:
Please Sign up or sign in to vote.
4.74/5 (33 votes)
19 Feb 20042 min read 357.5K   7.7K   163   78
Export Microsoft Outlook data using XML DataSets and the Outlook COM Object Library.

Image 1

Introduction

The Microsoft Outlook Connector is written in C# using the .NET 1.1 Framework. It attempts to abstract the data access with Microsoft Outlook and visual components using the data. Data from your Microsoft Outlook application can be exported to an XML file by simply checking the folder options and clicking Export. The source provides a simple example of mapping the COM object properties to a XML-friendly DataSet that could be used in any .NET application.

Background

This component for exporting Outlook objects stemmed from a couple years passively looking for a sync between my database and Outlook. I somehow stumbled on a VBA article on it (http://www.devasp.com/search/res/r9981.html) and decided to make a C# app to do the same.

Using the code

The primary function of the DataExportForm retrieves information from the user's Microsoft Outlook application via a custom connector which translates the Interop COM objects into basic DataSets. We should all be aware of how handy DataSets can be so lets look at how our form gets the information for the datagrid.

C#
private DataSet getCheckedItemSet()
{
    DataSet ds = new DataSet();
    OutlookConnector outlook = new OutlookConnector();

    // setup progress bars and process selected folders
    pgFolderProgress.Value = 0;
    outlook.ItemProcessed += new OutlookItemProcessed(outlook_ItemProcessed);
    pgFolderProgress.Maximum = lstExportObjects.CheckedItems.Count;
    foreach (ListViewItem obj in lstExportObjects.CheckedItems)
    {
        pgFolderProgress.Value++;
        pgItemProgress.Value = 0;

        switch (obj.Index) 
        {
            case 0:
                pgItemProgress.Maximum = outlook.getFolderCount(
                    Outlook.OlDefaultFolders.olFolderCalendar);
                ds.Merge(outlook.getCalendarDataSet());
                break;
            case 1:
                pgItemProgress.Maximum = outlook.getFolderCount(
                    Outlook.OlDefaultFolders.olFolderContacts);
                ds.Merge(outlook.getContactDataSet());
                break;
            case 2:
                pgItemProgress.Maximum = outlook.getFolderCount(
                    Outlook.OlDefaultFolders.olFolderInbox);
                ds.Merge(outlook.getInboxDataSet());
                break;
            case 3:
                pgItemProgress.Maximum = outlook.getFolderCount(
                    Outlook.OlDefaultFolders.olFolderNotes);
                ds.Merge(outlook.getNoteDataSet());
                break;
            case 4:
                pgItemProgress.Maximum = outlook.getFolderCount(
                    Outlook.OlDefaultFolders.olFolderTasks);
                ds.Merge(outlook.getTaskDataSet());
                break;
            default:
                Debug.WriteLine("Unsupported Export: " + obj.Index);
                break;
        }
    }
    outlook.Dispose();
    return ds;
}

Now how exactly does the OutlookConnector get it? You'll have to download the source to see the finer details of handling the Interop connection. Rest assured that it implements the IDisposable interface and works through MAPI to retrieve Outlook folder information. All of the Outlook connectivity is handled on instantiation which makes retrieval pretty easy, as seen here in the OutlookConnector.getContactDataSet() method.

C#
/// <summary>
/// Retrieves a list of all the Outlook Contacts.
/// </summary>
/// <returns>Contact Items DataSet</returns>
public DataSet getContactDataSet()
{
    Outlook.ContactItem item;
    DataSet rv = new DataSet();
    rv.DataSetName = "Contacts";
    rv.Tables.Add("Contact");
    rv.Tables[0].Columns.Add("FirstName");
    rv.Tables[0].Columns.Add("LastName");
    rv.Tables[0].Columns.Add("CompanyName");
    rv.Tables[0].Columns.Add("Email");
    rv.Tables[0].Columns.Add("HomePhone");
    rv.Tables[0].Columns.Add("WorkPhone");

    try
    {
        objFolder = objNamespace.GetDefaultFolder(
            Outlook.OlDefaultFolders.olFolderContacts);
        Debug.WriteLine(objFolder.Items.Count + " Contacts found.");
        foreach (System.Object _item in objFolder.Items) 
        {
            item = (Outlook.ContactItem) _item;
            rv.Tables[0].Rows.Add(new object[] {
                item.FirstName,
                item.LastName,
                item.CompanyName,
                item.Email1Address,
                item.HomeTelephoneNumber,
                item.BusinessTelephoneNumber
            });
            this.ItemProcessed();
        }
        Debug.WriteLine(rv.Tables[0].Rows.Count + " Contacts exported.");
    }
    catch (System.Exception e)
    {
        Console.WriteLine(e);
    }
    return rv;
}

Points of Interest

If your system does not have Microsoft Office Outlook 2003 you may have to change the References used by the "OutlookConnector" project. That is to say, if you received a build error described as "The type of namespace name 'Outlook' could not be found", you probably don't have Office 2003. Simply expand the project references, remove the afflicted items, and add the COM Library appropriate for your system. If someone has a dynamic way to handle this, I'd be curious to see you've done.

History

I wanted to limit this project to a single evening so it currently uses untyped DataSets. The real advantage of this application would come from using strong types and hooking in some DataAdapter(s) to really try to sync with some other system. I will probably end up tying in a MySQL data adapter in the coming weeks as time permits but if someone starts/finishes before I get to it, please let me know.

Version 1.1

Connector looping was switched to an incrementing localized integer since Office 10 does not implement a GetEnumerator() method. We have to use our own counter in a for loop instead. Also, here are some details on fixing your References between Office versions.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow to read large number of items from a folder? Pin
BlackMilan7-Apr-14 1:20
BlackMilan7-Apr-14 1:20 
BugSource code for get Inbox mail from outlook To Asp.net web page Pin
Manish Pandey19-Apr-13 0:29
Manish Pandey19-Apr-13 0:29 
Questionmicrosoft.office.core. missing? Pin
Haseeb Baber27-Feb-12 1:49
Haseeb Baber27-Feb-12 1:49 
AnswerRe: microsoft.office.core. missing? Pin
Haseeb Baber27-Feb-12 2:22
Haseeb Baber27-Feb-12 2:22 
Generalhu Pin
Member 779129431-Mar-11 5:43
Member 779129431-Mar-11 5:43 
GeneralSolution for the same item in every row Pin
Tom Klein17-Jan-11 0:31
Tom Klein17-Jan-11 0:31 
GeneralRe: Solution for the same item in every row Pin
Bakk Andris14-Mar-11 7:00
Bakk Andris14-Mar-11 7:00 
You have to call GetFirst before you call GetNext otherwise the first element won't be enumerated.
See the article in MSDN.

You should write the enumeration like this:
for (var item = (ContactItem)items.GetFirst(); item != null; item = (ContactItem) items.GetNext())

GeneralI tried to execute this code, it is giving an error "does not contain a definition for 'SenderEmailAddress'" Pin
Member 397979324-Jun-09 19:29
Member 397979324-Jun-09 19:29 
QuestionMicrosoft.Outlook11 missing Pin
disire8-Dec-08 22:21
disire8-Dec-08 22:21 
AnswerRe: Microsoft.Outlook11 missing Pin
tforsberg25-Feb-15 8:33
professionaltforsberg25-Feb-15 8:33 
QuestionHow can we do this in Asp.Net Pin
SKP2425-Mar-08 4:13
SKP2425-Mar-08 4:13 
Generalconvert "X.400/X.500 e-mail adress" into "Smtp e-mail adress" Pin
jackyontherock10-Aug-07 5:11
jackyontherock10-Aug-07 5:11 
GeneralExtracting/Importing [modified] Pin
ClaudeX6-Jun-07 22:57
ClaudeX6-Jun-07 22:57 
GeneralOutlook Namespace missing Pin
MarkChimes22-Feb-07 13:17
MarkChimes22-Feb-07 13:17 
GeneralRe: Outlook Namespace missing Pin
MarkChimes22-Feb-07 13:53
MarkChimes22-Feb-07 13:53 
GeneralExisting Outlook window closes ! Pin
ccangaroo24-Aug-06 3:29
ccangaroo24-Aug-06 3:29 
GeneralA bug Pin
nadav741-Jul-06 9:38
nadav741-Jul-06 9:38 
GeneralRe: A bug [modified] Pin
robocato31-Aug-06 1:19
robocato31-Aug-06 1:19 
GeneralPlease Help Pin
Ashisvadada14-Jun-06 17:36
Ashisvadada14-Jun-06 17:36 
QuestionProblem ??? Pin
- Pascal -16-May-06 12:06
- Pascal -16-May-06 12:06 
GeneralgoUsing this code with Exchange Pin
DeborahK28-Mar-06 9:18
DeborahK28-Mar-06 9:18 
GeneralDrag & Drop outlook contacts Pin
NewbieDude16-Mar-06 20:22
NewbieDude16-Mar-06 20:22 
GeneralProblem Pin
pro_8618-Feb-06 11:59
pro_8618-Feb-06 11:59 
QuestionHow about processing any PST? Pin
Matt Philmon14-Feb-06 6:35
Matt Philmon14-Feb-06 6:35 
GeneralBUG inside OutlookItemBuilder class and resolution. Pin
Preky6-Jul-05 21:51
Preky6-Jul-05 21:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.