Microsoft KB Archive/290658

= How to programmatically import Outlook items from Access =

Article ID: 290658

Article Last Modified on 11/24/2006

-

APPLIES TO


 * Microsoft Outlook 2002 Standard Edition
 * Microsoft Access 2002 Standard Edition

-



This article was previously published under Q290658







For a Microsoft Outlook 97 version of this article, see 170320.



For a Microsoft Outlook 98 version of this article, see 180981.



For a Microsoft Outlook 2000 version of this article, see 208232.



SUMMARY
This article provides an overview and sample code for creating Microsoft Outlook contacts from information stored in a Microsoft Access database. The sample code in the More Information section of this article should be run from a Microsoft Access global module and provides a starting point for the development of a complete solution.



MORE INFORMATION
The Import and Export feature in Outlook does not allow you to import or export information in user-defined fields (or properties). To import information into these fields, you may need to create a programming solution that uses Automation to convert the information.

The following sample code specifically converts information from Access into newly-created Outlook contacts, using Data Access Objects (DAO). With modifications, you can use this code as a basis for creating other types of Outlook items, such as appointments, notes, tasks, and such.

There is also an overview and separate code example available on the Microsoft Web site at the following location. It is the first example in the whitepaper.

http://msdn2.microsoft.com/en-us/office/aa905351.aspx

Programming Considerations

 * The following sample code uses the Northwind database included with Access 2002. If you installed Microsoft Office to a location other than the default, be sure to change the path to the database in the sample code.
 * You need to set a reference to the Microsoft Outlook 10.0 Object Library. A reference to the Microsoft DAO 3.6 Object Library, and the Microsoft Office 10.0 Object Library should be set by default.
 * The field types used in this example are text, in both Access and Outlook. To convert other types of fields, you must modify the code appropriately.
 * The contacts are automatically placed in the default Contact folder, which is at the same level as the Inbox.
 * The CreateItem method used in this example creates a new Outlook contact using the default form for the default contact folder. If you created a custom Outlook Contact form, you must set the MessageClass field for each item to the name of your custom form.

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please visit the following Microsoft Web site:

https://partner.microsoft.com/global/30000104

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:

http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS

Sample Code
Sub ExportAccessContactsToOutlook

' Set up DAO Objects. Dim oDataBase As DAO.Database Dim rst As DAO.Recordset Set oDataBase = OpenDatabase _ (&quot;c:\Program Files\Microsoft Office\Office10\Samples\Northwind.mdb&quot;) Set rst = oDataBase.OpenRecordset(&quot;Customers&quot;)

' Set up Outlook Objects. Dim ol As New Outlook.Application Dim olns As Outlook.Namespace Dim cf As Outlook.MAPIFolder Dim c As Outlook.ContactItem Dim Prop As Outlook.UserProperty

Set olns = ol.GetNamespace(&quot;MAPI&quot;) Set cf = olns.GetDefaultFolder(olFolderContacts)

With rst .MoveFirst

' Loop through the Microsoft Access records. Do While Not .EOF

' Create a new Contact item. Set c = ol.CreateItem(olContactItem)

' Specify which Outlook form to use. ' Change &quot;IPM.Contact&quot; to &quot;IPM.Contact. &quot; if you've        ' created a custom Contact form in Outlook. c.MessageClass = &quot;IPM.Contact&quot;

' Create all built-in Outlook fields. If ![CompanyName] <> &quot;&quot; Then c.CompanyName = ![CompanyName] If ![ContactName] <> &quot;&quot; Then c.FullName = ![ContactName]

' Create the first user property (UserField1). Set Prop = c.UserProperties.Add(&quot;UserField1&quot;, olText)

' Set its value. If ![CustomerID] <> &quot;&quot; Then Prop = ![CustomerID]

' Create the second user property (UserField2). Set Prop = c.UserProperties.Add(&quot;UserField2&quot;, olText)

' Set its value and so on.... If ![Region] <> &quot;&quot; Then Prop = ![Region]

' Save and close the contact. c.Save c.Close

.MoveNext Loop End With

End Sub

