1. Tech

Your suggestion is on its way!

An email with a link to:


was emailed to:

Thanks for sharing About.com with others!

Accessing Outlook Contacts
Suppose you used Delphi to write a little address database, and now you want to synchronize this database with the contacts stored in Microsoft Outlook. This article explains how.
 Win prizes by sharing code!
Do you have some Delphi code you want to share? Are you interested in winning a prize for your work?
Delphi Programming Quickies Contest
 Join the Discussion
"Post your views, comments, questions and doubts to this article."
 Related Resources
• Delphi takes control (over Word, Excel, Outlook, ...)
• Database development

Article submitted by Werner Pamler for the Delphi Programming Quickies Contest.

Outlook contacts
Suppose you used Delphi to write a little address database, and now you want to synchronize this database with the contacts stored in Microsoft Outlook. Some solutions to this problem exist on the web, but all of them take advantage of the COM objects provided by Microsoft. For the average Delphi programmer like me, COM is awkward: poorly documented, very confusing, and MUCH different from the Delphi objects.

Therefore, I was glad to find Turbo Power's OfficePartner components which wrap the entire COM representation of Outlook contacts into simple database components. In this way, the Outlook contacts can be addressed from Delphi like any other database, just as it is straightforward with BDE or ADO databases.

At first, install the OfficePartner components into Delphi. Recently, Turbo Power has released their Delphi components as open source. You can download a free version, including source code, from http://sourceforge.net/projects/tpofficepartner/. There you can also find a help file and an extensive, although a little out-dated, pdf documentation. Installation of the components themselves is straightforward.

Upon installation, Delphi's component palette will be extended by a new page "OfficePartner" which, among others, contains a TOpOutlook and a TOpContactsDataset component.

As a simple example, let's create a new Delphi project. Place the following components onto a form: a TDBGrid, a TDatasource, TOpContactsDataset, and a TOpOutlook component (Fig. 1).

Delphi using Outlook contacts

The OpOutlook component plays the role of a TDatabase component in the BDE or a TADOConnection in ADO - it simply provides a connection to Outlook.

The OpContactsDataset is nothing else like a standard dataset component found in any other database application, something like a TADOTable. Its property "Outlook" allows connecting it to the OpOutlook component, in the same way a TADOTable is connected to the TADOConnection. TOpContactsDataset directly inherits from TDataset and supports the basic dataset functionality, such as editing or deleting of records. More advanced functions such as sorting and filtering are not yet supported.

Click, click and let's rool
And of course, as usual, set the "Dataset" property of the TDatasource to point to the OpContactsDataset, and set the "Datasource" property of the TDBGrid to point to the TDatasource. When you set the "Connected" property of TOpOutlook and the "Active" property of TOpContactsDataset to true, you will see the Outlook contacts to appear in the DBGrid. No line of code - that's what we like in Delphi!

Hold on, there's more! Outlook contact records have many fields which may not be needed simultaneously in a Delphi application. As a rough pre-selection, TOpContactsDataset is equipped with the property "ContactInfo" which allows including default data ("citDefault"), business data ("citBusiness") and/or personal data ("citPersonal") etc. in the dataset.

With the default setting of the "ListSource" property ("cltContacts") the dataset will have access to the default contact folder. If the Outlook folder tree has additional contact folders, these can be selected by setting "ListSource" to "cltFolders" and specifying the path to the requested folder in "ListName". Suppose your Outlook installation has a personal folder named "Personal" containing a contacts folder named "VIP", then the string "\Personal\VIP" will be the necessary text to be entered in "ListName".

Hmm, no Outlook? Don't worry!
A final word if there is a chance that your Delphi program will have to run on systems without Outlook: in this case, don't use the object inspector to connect to Outlook and to activate the ContactsDataset, but use the form's OnCreate event instead. This avoids the program crash occurring on such systems at program start. Just intercept the exception raised and notify the user of the installation problem.

procedure TForm1.FormCreate(Sender:TObject);
    OpOutlook1.Connected := true;
    OpContactsDataset1.Active := true;
    MessageDlg('Cannot connect to Outlook.'+
      'Probably, Outlook is not installed on this machine.',
      mtError, [mbOK], 0]; 

That's it.
As simple as can be! If you want to discuss Delphi + Outlook related topics, please post to the Delphi Programming Forum.

Explore Delphi
By Category
    delphiDelphicomputeTechb0503636f2000405a6811db6b0503636f2000505a680411ahttp://delphi.about.comod526F6F743522terminatedZarko Gajicdelphiguide36W00062zNIP11970-01-0110/od/index.htm0526F6F741approved/od
  1. About.com
  2. Tech
  3. Delphi
  4. Advanced Delphi Techniques
  5. OLE / COM / Automation
  6. Automation
  7. Accessing Outlook Contacts

©2015 About.com. All rights reserved.