Results 1 to 6 of 6

Thread: VBA to export Contacts to Access

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #6
    VBAX Regular
    Joined
    Jul 2018
    Posts
    17
    Location
    Finally solved.

    Thanks for the links, which led me to a solution which I have tweaked and have copied below in case anyone else needs to do this. Everything is simple . . . if you know how !

    It was far easier to write the code in Access than in Outlook. Below is the Access Module which does the importing - I hope it is self-explanatory:

    Two other things are needed:

    1. You need to know the required field names in Outlook Contacts - I found them here: https://msdn.microsoft.com/en-us/vba...ent-properties
    2. In the VBA window you need to add a reference: Tools . . . References . . . Microsoft Outlook <version number> Object Library

    Sub ImportContactsFromOutlook()
        ' To import selected fields from Contacts in Outlook and pllace in Access table "Contacts"
        ' Set up DAO objects (uses existing "Contacts" table)
        Dim rst As DAO.Recordset
        Set rst = CurrentDb.OpenRecordset("Contacts")
        ' 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
        ' Clear the existing Contacts table                                     
        ' Otherwise the imported records are appended to the existing records
        strTableName = "Contacts"                                         
        ' Name the Table to be cleared
        DoCmd.SetWarnings False                                          
        ' Stop warnings about deletions
        DoCmd.RunSQL "DELETE *.* FROM " & strTableName   
        ' Do the deletion
        DoCmd.SetWarnings True                                           
        ' Turn warnings on again
        DoCmd.Close acTable, "Contacts", acSaveYes               
        ' Close the cleared Table
        ' Import the required fields froom Outlook
        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
                     ' Custom Outlook properties look like this:
                     ' rst!AccessFieldName = c.UserProperties("OutlookPropertyName")
                     rst!FirstName = c.FirstName
                     rst!LastName = c.LastName
                     rst!HomeStreet = c.HomeAddressStreet
                     rst!HomeCity = c.HomeAddressCity
                     rst!HomeState = c.HomeAddressState
                     rst!HomePostalCode = c.HomeAddressPostalCode
                     rst!HomeCountryRegion = c.HomeAddressCountry
                     rst!Categories = c.Categories
                     rst!HomePhone = c.HomeTelephoneNumber
                     rst!MobilePhone = c.MobileTelephoneNumber
                     rst.Update
                 End If
            Next i
            rst.Close     
            DoCmd.Close acTable, "Contacts", acSaveYes         
            ' Close the populated Table
        Else
             MsgBox "No contacts to export."
       End If
    End Sub
    Last edited by Aussiebear; 03-12-2025 at 04:35 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •