Results 1 to 1 of 1

Thread: [FAQ's: OD] How do I Export ContactItems to a Access database?

  1. #1

    Thread Starter
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,713

    [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:
    1. 'Behind ThisOutlookSession
    2.  
    3. Option Explicit
    4. 'Add a reference to MS ActiveX Data Objects 2.x Library
    5. Public Sub ExportContacts()
    6.    
    7.     'Connect to your database
    8.     Dim oCnn As ADODB.Connection
    9.    
    10.     Set oCnn = New ADODB.Connection
    11.     oCnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyDB.mdb;User Id=Admin;Password=;"
    12.     oCnn.Open
    13.  
    14.     'Connect to your default Contacts folder
    15.     Dim oConFolder As Outlook.MAPIFolder
    16.     Dim oContact As Outlook.ContactItem
    17.     Dim sSQL As String
    18.  
    19.     Set oConFolder = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderContacts)
    20.     'Iterate through the primary Outlook Contacts
    21.     For Each oContact In oConFolder.Items
    22.         'Table1 must already exist with the correct fields/Data types
    23.         sSQL = "INSERT INTO Table1"
    24.         sSQL = sSQL & " (FirstName, LastName, CompanyName, Email1Address, Phone, FAX)"
    25.         sSQL = sSQL & " VALUES('" & oContact.FirstName '
    26.         sSQL = sSQL & "', '" & oContact.LastName  '
    27.         sSQL = sSQL & "', '" & oContact.CompanyName  '
    28.         sSQL = sSQL & "', '" & oContact.Email1Address  '
    29.         sSQL = sSQL & "', '" & oContact.BusinessTelephoneNumber  '
    30.         sSQL = sSQL & "', '" & oContact.BusinessFaxNumber & "')"  '
    31.         oCnn.Execute sSQL
    32.     Next
    33.     Set oContact = Nothing
    34.     Set oConFolder = Nothing
    35.     oCnn.Close
    36.     Set oCnn = Nothing
    37.  
    38. End Sub
    Last edited by RobDog888; May 22nd, 2006 at 03:11 AM.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

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



Click Here to Expand Forum to Full Width