Click here to Skip to main content
6,595,854 members and growing! (18,238 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate

.NET Connector for Microsoft Outlook

By Mathias Taylor

Export Microsoft Outlook data using XML DataSets and the Outlook COM Object Library.
C++, C#.NET 1.1, Win2K, WinXP, Win2003, MFC, VS.NET2003, Dev
Posted:22 Nov 2003
Updated:19 Feb 2004
Views:186,751
Bookmarked:135 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
28 votes for this article.
Popularity: 6.55 Rating: 4.52 out of 5

1

2
2 votes, 7.1%
3
8 votes, 28.6%
4
18 votes, 64.3%
5

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.

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.

/// <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

About the Author

Mathias Taylor


Member

Occupation: Web Developer
Location: United States United States

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 70 (Total in Forum: 70) (Refresh)FirstPrevNext
GeneralI tried to execute this code, it is giving an error "does not contain a definition for 'SenderEmailAddress'" PinmemberMember 397979320:29 24 Jun '09  
QuestionMicrosoft.Outlook11 missing Pinmemberdisire23:21 8 Dec '08  
QuestionHow can we do this in Asp.Net PinmemberSKP245:13 25 Mar '08  
Generalconvert "X.400/X.500 e-mail adress" into "Smtp e-mail adress" Pinmemberjackyontherock6:11 10 Aug '07  
GeneralExtracting/Importing [modified] PinmemberClaudeX23:57 6 Jun '07  
GeneralOutlook Namespace missing PinmemberMarkChimes214:17 2 Feb '07  
GeneralRe: Outlook Namespace missing PinmemberMarkChimes214:53 2 Feb '07  
GeneralExisting Outlook window closes ! Pinmemberccangaroo4:29 24 Aug '06  
GeneralA bug Pinmembernadav7410:38 1 Jul '06  
GeneralRe: A bug [modified] Pinmemberrobocato2:19 31 Aug '06  
GeneralPlease Help PinmemberAshisvadada18:36 14 Jun '06  
GeneralProblem ??? PinmemberPascal Groulx13:06 16 May '06  
GeneralgoUsing this code with Exchange PinmemberDeborahK10:18 28 Mar '06  
GeneralDrag & Drop outlook contacts PinmemberNewbieDude21:22 16 Mar '06  
GeneralProblem Pinmemberpro_8612:59 18 Feb '06  
GeneralHow about processing any PST? PinmemberMatt Philmon7:35 14 Feb '06  
GeneralBUG inside OutlookItemBuilder class and resolution. PinmemberPreky22:51 6 Jul '05  
GeneralAlways return 1 item Pinmembersksandz2:08 8 Apr '05  
GeneralRe: Always return 1 item PinmemberCtrAltDel11:22 30 Apr '05  
GeneralRe: Always return 1 item Pinmembereyop@eyop.co.kr23:09 14 Sep '05  
GeneralRe: Always return 1 item PinmemberMember 33064027:58 1 Jan '08  
GeneralNullReferenceException when running the code Pinmemberpvsunil1:32 8 Apr '05  
Generalexporting e-mails of additional mailbox PinsussMagicGirL8317:24 30 Mar '05  
Generaldisplay the exported e-mails on a web page PinsussMagicGirL8314:38 30 Mar '05  
GeneralMS Outlook FAXMaker PinmemberFayeeg0:41 2 Feb '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 19 Feb 2004
Editor: Nishant Sivakumar
Copyright 2003 by Mathias Taylor
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project