Export Outlook Contacts to Excel


September 6, 2008 – 9:02 pm by JP

    Here is the last of the add-in code I was working on. This piece extracts the properties from Contact Items into Excel, using early binding to speed up the code, but with GetObject/CreateObject calls to attempt to hook into an existing instance of Outlook. What, do you mean you don't leave Outlook running all day while you're in the office?

    This code uses the same technique from the Tasks extracting code I posted earlier, so this code below should look pretty familiar to you if you check out that post. An array is populated with the contact information, which is then dumped in one shot to the worksheet.

    Something seems to be wrong with my code syntax highlighter, you'll need to cut and paste the code in order to view it proper. It does cut and paste properly, even though you can't view it all.

    I haven't tested this but it should work as written. This code is just a demonstration with three fields (Company name, Country and Phone Number). If you want to export more fields, look up the properties of the ContactItem here.

    I also included some additional code at the bottom which can be used in other routines to prompt the user to save the file, with a sample filename, filter and title so you can see how code like that would work. It also handles the possibility that the user clicks "Yes" to save the file, but then presses Cancel in the file save dialog box (or doesn't type anything and presses OK).

VBA:
  1. Sub ExtractContacts()
  2. '
  3. ' If Outlook is not open, it still works, but much slower (~8 secs vs. 2 secs w/ Outlook open).
  4. '
  5. Dim olApp As Outlook.Application
  6. Dim olNS As Outlook.Namespace
  7. Dim myContactItems As Outlook.Items
  8. Dim ThisContact As Outlook.ContactItem
  9. Dim MyBook As Excel.Workbook
  10. Dim rngStart As Excel.Range
  11. Dim rngHeader As Excel.Range
  12. Dim FileToSave As String
  13. Dim NextRow As Long
  14. Dim ColCount As Long
  15. Dim i As Long
  16. Dim arrData() As Variant
  17.  
  18. Application.ScreenUpdating = False
  19.  
  20. ' get or create Outlook object and make sure it exists before continuing
  21. On Error Resume Next
  22. Set olApp = GetObject(, "Outlook.Application")
  23. If Err.Number <> 0 Then
  24.   Set olApp = CreateObject("Outlook.Application")
  25. End If
  26. On Error GoTo 0
  27. If olApp Is Nothing Then
  28.   MsgBox "Cannot start Outlook.", vbExclamation, APPNAME
  29.   GoTo ExitProc
  30. End If
  31.  
  32. Set olNS = olApp.GetNamespace("MAPI")
  33. Set myContactItems = olNS.GetDefaultFolder(olFolderContacts).Items
  34.  
  35. If myContactItems.Count> 0 Then
  36.  
  37.   Set MyBook = Excel.Workbooks.Add
  38.   MyBook.Sheets(1).Name = "Contacts"
  39.   Set rngStart = MyBook.Sheets(1).Range("A1")
  40.   Set rngHeader = Range(rngstart, rngstart.Offset(0,3))
  41.  
  42.   ' with assistance from Jon Peltier http://peltiertech.com/WordPress and
  43.   ' http://support.microsoft.com/kb/306022
  44.  
  45.   rngHeader.Value = Array("Company Name", "Country", "Telephone Number")
  46.  
  47.   ColCount = rngHeader.Columns.Count
  48.  
  49.   ' now that we know how many rows and columns we need,
  50.   ' resize the array accordingly
  51.   ReDim arrData(1 To myContactItems.Count, 1 To ColCount)
  52.  
  53. '
  54. ' to make it more obvious:
  55. ' Dim LastRow As Long
  56. ' Dim LastColumn As Long
  57. ' LastRow = myContactItems.Count
  58. ' LastColumn = rngHeader.Columns.Count
  59. ' ReDim arrData( 1 to LastRow, 1 to LastColumn)
  60. '
  61. '
  62.   For i = 1 to myContactItems.Count
  63.     Set ThisContact = myContactItems.item(i)
  64.  
  65.     arrData(i, 1) = ThisContact.CompanyName
  66.     arrData(i, 2) = ThisContact.HomeAddressCountry
  67.     arrData(i, 3) = ThisContact.BusinessTelephoneNumber
  68.  
  69.   Next i
  70.  
  71.   rngStart.Offset(1, 0).Resize(myContactItems.Count, ColCount).Value = arrData
  72.  
  73.  
  74. Else
  75.   MsgBox "I don't see any contacts in your default Contacts folder. Exiting now...", vbOKOnly, APPNAME
  76. End If
  77.  
  78. If MsgBox("Would you like to save the exported contacts list now?", vbInformation + vbYesNo) = vbYes Then
  79.   FileToSave = Application.GetSaveAsFilename("Outlook Contacts", FileFilter:="Microsoft Office Excel Workbook (*.xls), *.xls", Title:="Save File")
  80.       If FileToSave <> False Then
  81.         ActiveWorkbook.SaveAs FileToSave, FileFormat:=xlNormal
  82.       End If
  83. End If
  84.  
  85. ExitProc:
  86. Application.ScreenUpdating = True
  87. Set ThisContact = Nothing
  88. Set rngStart = Nothing
  89. Set MyBook = Nothing
  90. Set olNS = Nothing
  91. Set olApp = Nothing
  92. Set myContactItems = Nothing
  93. Erase arrData
  94. End Sub

Enjoy,
JP


Share and Enjoy:
  • StumbleUpon
  • Technorati
  • Digg
  • Google
  • del.icio.us
  • MisterWong

Print This Post Print This Post  |  Email This Post Email This Post  |  Permalink  |  Subscribe to this feed Subscribe now!

Filed Under: Excel, Outlook, VBA, automation
Tags: , , ,

This post has 459 views since September 6, 2008 – 9:02 pm.

Post a Comment

To post VBA code in your comment, use [VBA] tags, like this: [VBA]Code goes here[/VBA].





Subscribe without commenting

Keep Reading:

Browse Posts:


« Reading worksheet values into arrays || Excel Blogs List and OPML Feeds List »