Become a Columnist Microsoft Exchange Site Microsoft Support SiteMSDN Exchange Site

       How did you like this article? Please vote and let us know.          

Subscribe to OutlookExchange
Anderson Patricio
Ann Mc Donough
Bob Spurzem
Brian Veal
Catherine Creary
Cherry Beado
Colin Janssen
Collins Timothy Mutesaria
Drew Nicholson
Fred Volking
Glen Scales
Goran Husman
Guy Thomas
Henrik Walther
Jason Sherry
Jayme Bowers
John Young
Joyce Tang
Justin Braun
Konstantin Zheludev
Kristina Waters
Kuang Zhang
Mahmoud Magdy
Martin Tuip
Michael Dong
Michele Deo
Mitch Tulloch
Nicolas Blank
Pavel Nagaev
Ragnar Harper
Ricardo Silva
Richard Wakeman
Russ Iuliano
Santhosh Hanumanthappa
Shannal L. Thomas
Steve Bryant
Steve Craig
Todd Walker
Tracey J. Rosenblath

 

 
 
Exporting a Microsoft Access Table into an Outlook Contacts Folder

This section shows you how to add a list of contacts, stored in a Microsoft Access table, to your Outlook Contacts folder. It uses a subroutine, ExportContactsTable and a function, boolCheckName.

The subroutine,ExportContactsTable, creates a new Outlook ContactItem object for each record in the table. It takes a table name as its only argument. The table should have the fields: ContactName, Address, City, Region, PostalCode, Country, Phone, Fax, CompanyName, ContactTitle, and CustomerId.

In the sample table there is a field, CustomId, which does not correspond to any of Outlook's built-in fields. The subroutine creates a custom field for an Outlook ContactItem. This custom field will be available as a User-defined field in Outlook through the Field Chooser.

The function, boolCheckName is called by ExportContactsTable to do some minimal checking of each contact name. It insures that the name is not a zero length string and that it does not already exist in the Outlook Contacts list. It returns True if the contact should be added to the Contact List.

The sample code for this section as well as a sample table of contacts is in AccSamp.mdb. The code is in the modContacts module. To run this solution, open the database and click Export Contacts on the Samples form.

Note Running the sample code adds thirteen contacts to your Outlook Contact List. However, each contact name added to your list is preceded with the word "test" for easy deletion.

Note To use this code in your own project, you must reference the Microsoft Outlook 8.0 Object Library.

 


 

 
'Use a global constant for the message box caption.
Const MESSAGE_CAPTION = "Exporting Contacts to Microsoft Access"
 
Public Sub ExportContactsTable(strTableName As String)
 
	Dim oOutlook As New Outlook.Application
	Dim colItems As Items
	Dim tblContacts As Recordset
	Dim upContactId As UserProperty
	Dim strMessage as String
	
	Const ERR_TABLE_NOT_FOUND = 3078
	Const ERR_FIELD_NOT_FOUND = 3265
	Const ERR_ATTACHED_TABLE_NOT_FOUND = 3024
	Const ERR_INVALID_ATTACHED_TABLE_PATH = 3044

	On Error GoTo ERR_ExportContactsTable

	'Open the table.
	Set tblContacts = CurrentDb.OpenRecordset(strTableName)

	'Get a reference to the Items collection of the contacts folder.
	Set colItems = oOutlook.GetNamespace("MAPI"). _
						GetDefaultFolder(olFolderContacts).Items

		Do Until tblContacts.EOF
			If boolCheckName(Nz(tblContacts!ContactName), colItems) Then
				'Use the Add method of Items collection to fill in the 
				'fields with the data from the table and then save the new 
				'item.
				With colItems.Add
					.FullName = Nz(tblContacts!ContactName)
					.BusinessAddressStreet = Nz(tblContacts!Address)
					.BusinessAddressCity = Nz(tblContacts!City)
					.BusinessAddressState = Nz(tblContacts!Region)
					.BusinessAddressPostalCode = Nz(tblContacts!PostalCode)
					.BusinessAddressCountry = Nz(tblContacts!Country)
					.BusinessTelephoneNumber = Nz(tblContacts!Phone)
					.BusinessFaxNumber = Nz(tblContacts!Fax)
					.CompanyName = Nz(tblContacts!CompanyName)
					.JobTitle = Nz(tblContacts!ContactTitle)

					'Create a custom field.
					Set upContactId = .UserProperties. _ 
							Add("ContactID", olText)

					upContactId = Nz(tblContacts![CustomerID])

					.Save
				End With
			End If
			tblContacts.MoveNext
		Loop
		tblContacts.Close
		
		strMessage = "Your contacts have been successfully exported."
		MsgBox strMessage, vbOKOnly, MESSAGE_CAPTION
Exit_ExportContactsTable:
	On Error Resume Next

	Set tblContacts = Nothing
	Set oOutlook = Nothing

	Exit Sub 
ERR_ExportContactsTable:

	Select Case Err
		Case ERR_TABLE_NOT_FOUND
			strMessage = "Cannot find table!"
			MsgBox strMessage, vbCritical, MESSAGE_CAPTION
			Resume Exit_ExportContactsTable

			'These errors occur if an attached table is moved or deleted
			'or if the path to the table file is no longer valid.
			Case ERR_ATTACHED_TABLE_NOT_FOUND, ERR_INVALID_ATTACHED_TABLE_PATH
				strMessage = "Cannot find attached table!"
				MsgBox strMessage, vbCritical, MESSAGE_CAPTION
				Resume Exit_ExportContactsTable

			'If a field in the code does not match a field in the table
			'then move on to the next field.
			Case ERR_FIELD_NOT_FOUND
				Resume Next
			Case Else
				strMessage = "An unexpected error has occured. Error#" _
							& Err & ": " & Error
				MsgBox strMessage, vbCritical, MESSAGE_CAPTION
				Resume Exit_ExportContactsTable
		End Select

End Sub

Function boolCheckName(strName As String, colItems As Items) _
					As Boolean

	Dim varSearchItem As Variant
	Dim strMessage As String

	If Len(strName) = 0 Then
		strMessage = "This record is missing a full name. "
		strMessage = strMessage & "Do you want to add it anyway?"
		If MsgBox(strMessage, vbYesNo, MESSAGE_CAPTION) = vbYes Then
			boolCheckName = True
		Else
			boolCheckName = False
		End If
	Else
		'Find the first item that has a FullName equal to strName. If no
		'item is found, varSearchItem wil be equal to Nothing.
		Set varSearchItem = colItems.Find("[FullName] = """ & strName & """")
		If varSearchItem Is Nothing Then
			boolCheckName = True
		Else
			strMessage = "A contact named " & strName & " already exists. "
			strMessage = strMessage & _
						"Do you want to add this contact anyway?"

			If MsgBox(strMessage, vbYesNo, MESSAGE_CAPTION) = vbYes Then
			boolCheckName = True
			Else
				boolCheckName = False
			End If
		End If
	End If
End Function



Disclaimer: Your use of the information contained in these pages is at your sole risk. All information on these pages is provided "as is", without any warranty, whether express or implied, of its accuracy, completeness, fitness for a particular purpose, title or non-infringement, and none of the third-party products or information mentioned in the work are authored, recommended, supported or guaranteed by Pro Exchange. OutlookExchange.Com and Pro Exchange shall not be liable for any damages you may sustain by using this information, whether direct, indirect, special, incidental or consequential, even if it has been advised of the possibility of such damages.

© Copyright Pro Exchange, Inc., 2006