How to programmatically export Outlook items to Access (290792)



The information in this article applies to:

  • Microsoft Outlook 2002
  • Microsoft Access 2002

This article was previously published under Q290792
For a Microsoft Outlook 2000 version of this article, see 253794.

SUMMARY

This article provides an overview and sample code for importing Outlook contact information into an Access table. The sample code in the "More Information" section of this article should be run from an Access global module. The code provides a starting point for the development of a complete solution.

MORE INFORMATION

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

NOTE: One alternative to creating code to export user-defined fields is to add all of the fields to a table view in Outlook. You can then select all of the data and copy and paste it into Microsoft Excel.

The following sample code converts Outlook contact information into Access database table rows. With modifications, you can use this code as a basis for importing other types of Outlook items, such as appointments, notes, tasks, and so on.

There is also an overview and separate code example available on the Microsoft Web site at the following location:

Programming Considerations

  • You need to set a reference to the Microsoft Outlook 10.0 Object Library and the Microsoft DAO 3.6 Object Library.
  • 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 sample code provided does not include error checking code which is necessary for a production application.
  • The code below assumes that the Access fields are set to Allow Zero-Length Values.
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: For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:

Sample Code

Sub ImportContactsFromOutlook()

   ' This code is based in Microsoft Access.

   ' Set up DAO objects (uses existing "tblContacts" table)
   Dim rst As DAO.Recordset
   Set rst = CurrentDb.OpenRecordset("tblContacts")


   ' 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 objItems As Outlook.Items
   Dim Prop As Outlook.UserProperty

   Set olns = ol.GetNamespace("MAPI")
   Set cf = olns.GetDefaultFolder(olFolderContacts)
   Set objItems = cf.Items
   iNumContacts = objItems.Count
   If iNumContacts <> 0 Then
      For i = 1 To iNumContacts
         If TypeName(objItems(i)) = "ContactItem" Then
            Set c = objItems(i)
            rst.AddNew
            rst!FirstName = c.FirstName
            rst!LastName = c.LastName
            rst!Address = c.BusinessAddressStreet
            rst!City = c.BusinessAddressCity
            rst!State = c.BusinessAddressState
            rst!Zip_Code = c.BusinessAddressPostalCode
            ' Custom Outlook properties would look like this:
            ' rst!AccessFieldName = c.UserProperties("OutlookPropertyName")
            rst.Update
         End If
      Next i
      rst.Close
      MsgBox "Finished."
   Else
      MsgBox "No contacts to export."
   End If

End Sub
				

REFERENCES

For additional information about importing data from Access to Outlook, click the article number below to view the article in the Microsoft Knowledge Base:

290658 OL2002: How to Programmatically Import Outlook Items from Microsoft Access

For additional information about available resources and answersto commonly asked questions about Microsoft Outlook solutions, click the article number below to view the article in the Microsoft Knowledge Base:

287530 OL2002: Questions About Custom Forms and Outlook Solutions


Modification Type:MajorLast Reviewed:8/4/2005
Keywords:kbDatabase kbExport kbProgramming KbVBA kbhowto KB290792