Export All Contacts as vCards

Outlook has the built in ability to export contacts as vCards, but it will only do it one at a time. With the following vba script and a few bash commands, you can batch export each contact as a vCard and then combine the individual files into one vcard file.

' Copyright under GPL by Mark Grimes
' Batch export contacts as vCards
Sub ExportVCards()

    Dim objNS As NameSpace
    Dim objFolders, objFolder, objContactFolder
    Dim objEntry As Variant
    Dim objContactEntry As ContactItem
    Dim count As Integer

    count = 0

    Set objNS = Application.GetNamespace("MAPI")
    Set objContactFolder = objNS.GetDefaultFolder(olFolderContacts)
    ' Set objCalFolder = objNS.Folders.item("Mailbox - Mark").Folders.item("Calendar")

    For Each objEntry In objContactFolder.Items
        If Not TypeOf objEntry Is ContactItem Then
            If TypeOf objEntry Is DistListItem Then
                Debug.Print "Found a distribution list, skipping"
            Else
                Debug.Print "****** found a something odd ****"
                Debug.Print "  " & objEntry
            End If
        Else

            Set objContactEntry = objEntry
            count = count + 1
            Debug.Print count & ": " & objContactEntry.Subject

            path = "/tmp/contacts/contact" & count & ".vcf"
            objContactEntry.SaveAs path, olVCard
        End If
    Next
    Set objNS = Nothing

End Sub

And a few bash commands to combine the files into one:

cd /tmp/contacts
cat contacts*.vcf > outlook-contacts.vcf
rm -f contacts*.vcf

Miquel Aguado suggested the following win/dos batch command to create a single file:

c:> type c:\temp\contacts\contact*.vcf >> c:\temp\contacts\allcontacts.vcf

And he generously provided these modifications to the subroutine to have the command called directly from Outlook. I have not tested this, but it looks like it would work just fine.

# -----------
# Put the following before the for-loop
   Dim strOutputDirectory, strOutputFilePrefix, strOutputFileSuffix As String
   strTypeCommand = "c:\windows\system32\cmd.exe /c "
   strOutputDirectory = "c:\temp\contacts\"
   strOutputFilePrefix = "contact"
   strOutputFileSuffix = ".vcf"
   strOutputFileName = "allContacts.vcf"
# code end
# -----------
# modify the assignment to var path with the following
   Path = strOutputDirectory & strOutputFilePrefix & count & strOutputFileSuffix
# code end
# -----------
# Put the following at the end of the method
   Dim strCommand As String
   strCommand = strTypeCommand & " """ & strOutputDirectory &
        strOutputFilePrefix & "*" & strOutputFileSuffix & """ >> """ &
        strOutputDirectory & strOutputFileName & """"
   Debug.Print strCommand
   Call Shell(strCommand, 0)
# code end
# -----------

Published

January 24, 2009 7:00PM

License

The contents of this blog are licensed under the Creative Commons “Attribution-Noncommercial-Share Alike 3.0″ license.