[FAQ's: OD] How do I Export ContactItems to a Access database?
Using ADO for this example we can easily export our primary Outlook contacts to an Access database. You can use any technology to perform the database connection and inserts.
Outlook VBA Code Example:
VB Code:
'Behind ThisOutlookSession
Option Explicit
'Add a reference to MS ActiveX Data Objects 2.x Library
Public Sub ExportContacts()
'Connect to your database
Dim oCnn As ADODB.Connection
Set oCnn = New ADODB.Connection
oCnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyDB.mdb;User Id=Admin;Password=;"
oCnn.Open
'Connect to your default Contacts folder
Dim oConFolder As Outlook.MAPIFolder
Dim oContact As Outlook.ContactItem
Dim sSQL As String
Set oConFolder = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderContacts)
'Iterate through the primary Outlook Contacts
For Each oContact In oConFolder.Items
'Table1 must already exist with the correct fields/Data types
sSQL = "INSERT INTO Table1"
sSQL = sSQL & " (FirstName, LastName, CompanyName, Email1Address, Phone, FAX)"
sSQL = sSQL & " VALUES('" & oContact.FirstName '
sSQL = sSQL & "', '" & oContact.LastName '
sSQL = sSQL & "', '" & oContact.CompanyName '
sSQL = sSQL & "', '" & oContact.Email1Address '
sSQL = sSQL & "', '" & oContact.BusinessTelephoneNumber '
sSQL = sSQL & "', '" & oContact.BusinessFaxNumber & "')" '
oCnn.Execute sSQL
Next
Set oContact = Nothing
Set oConFolder = Nothing
oCnn.Close
Set oCnn = Nothing
End Sub