views:

348

answers:

1

All,

I'm trying to write a program that will export all the contacts stored in the local pst file of outlook to vcards

I have seen several shareware programs that will do it but they seem pretty expensive (50 bucks+)

I have seen an example here but it seems more geared to an exchange server rather than to the local install of outlook

any ideas on how i would go about this?

+2  A: 

This may suit.

Sub VCardOut()
Dim oFolder As Object
Dim oContact As ContactItem
Dim sPath As String
Dim sName As String
Dim sVCard As String
Dim f As Object
Dim fs As Object
Dim i As Integer


    Set fs = CreateObject("Scripting.FileSystemObject")
    Set oFolder = GetNamespace("MAPI").GetDefaultFolder(olFolderContacts)

    sPath = "C:\Docs\"

'Problem with formatting and backslash

    '' Loop through all of the items in the folder.
    For i = 1 To oFolder.Items.Count
        Set oContact = oFolder.Items(i)

        sName = oContact.FullNameAndCompany & ".vcf"

        If Trim(sName) = ".vcf" Then sName = oContact.EntryID & ".vcf"

        Set f = fs.CreateTextFile(sPath & sName)

        sVCard = "BEGIN:VCARD" & vbCrLf
        sVCard = sVCard & "VERSION:2.1" & vbCrLf

        sVCard = sVCard & "FN:" & oContact.FullName & vbCrLf

        sVCard = sVCard & "N:" & oContact.LastName & ";" & oContact.FirstName & ";" _
            & oContact.MiddleName & ";" & oContact.Title & ";" & vbCrLf

        sVCard = sVCard & "NICKNAME:" & oContact.NickName & vbCrLf

        sVCard = sVCard & "ADR;HOME;ENCODING=QUOTED-PRINTABLE:;;" _
            & Replace(oContact.HomeAddress & "", vbCrLf, "=0D=0A") & ";" _
            & Replace(oContact.HomeAddressCity & "", vbCrLf, "=0D=0A") & ";" _
            & Replace(oContact.HomeAddressCountry & "", vbCrLf, "=0D=0A") & vbCrLf

        sVCard = sVCard & "ADR;WORK;ENCODING=QUOTED-PRINTABLE:;;" _
            & Replace(oContact.BusinessAddress & "", vbCrLf, "=0D=0A") & ";" _
            & Replace(oContact.BusinessAddressCity & "", vbCrLf, "=0D=0A") & ";" _
            & Replace(oContact.BusinessAddressCountry & "", vbCrLf, "=0D=0A") & vbCrLf

        sVCard = sVCard & "BDAY:" & Format(oContact.Birthday, "yyyymmdd") & vbCrLf

        sVCard = sVCard & "EMAIL;PREF;INTERNET:" & oContact.Email1Address & vbCrLf

        '' Repeat as necessary for each email address
        sVCard = sVCard & "EMAIL;INTERNET:" & oContact.Email2Address & vbCrLf

        sVCard = sVCard & "ORG:" & oContact.CompanyName & ";" & oContact.Department & vbCrLf

        sVCard = sVCard & "TEL;CELL;VOICE:" & oContact.MobileTelephoneNumber & vbCrLf

        sVCard = sVCard & "TEL;HOME;FAX:" & oContact.HomeFaxNumber & vbCrLf

        sVCard = sVCard & "TEL;HOME;VOICE:" & oContact.HomeTelephoneNumber & vbCrLf

        sVCard = sVCard & "TEL;WORK;FAX:" & oContact.BusinessFaxNumber & vbCrLf

        sVCard = sVCard & "TEL;WORK;VOICE:" & oContact.BusinessTelephoneNumber & vbCrLf

        sVCard = sVCard & "TITLE:" & oContact.JobTitle & vbCrLf

        sVCard = sVCard & "URL;HOME:" & oContact.PersonalHomePage & vbCrLf

        sVCard = sVCard & "URL;WORK:" & oContact.BusinessHomePage & vbCrLf

        sVCard = sVCard & "REV:20090225T232716Z" & vbCrLf
        sVCard = sVCard & "End: VCARD"

        f.WriteLine sVCard
        f.Close
    Next

End Sub
Remou