Sunday, February 21, 2010

How to Import Multiple vCard Files in MS Outlook

Source of this post

You can import multiple vCard files, the ones with the extension .vcf, in the Microsoft Office Outlook using a VBA macro. Just follow the steps below:

  1. Create a folder on the root of the C: drive and name it VCARDS.
  2. Copy all your individual vCard files (.vcf) to this newly created folder.
  3. Open Outlook and click ALT + F11 to open the VBA editor.
  4. Click TOOLS --> REFERENCES and then select Microsoft Scripting Runtime and Windows Script Host Object Model from the list and place checks in the box next to each and click OK.
  5. Click INSERT --> MODULE and copy and paste the code below into the blank module and save it.

    Sub OpenSaveVCard()
    Dim objWSHShell As IWshRuntimeLibrary.IWshShell
    Dim objOL As Outlook.Application
    Dim colInsp As Outlook.Inspectors
    Dim strVCName As String
    Dim fso As Scripting.FileSystemObject
    Dim fsDir As Scripting.Folder
    Dim fsFile As Scripting.File
    Dim vCounter As Integer
    Set fso = New Scripting.FileSystemObject
    Set fsDir = fso.GetFolder("C:\VCARDS")

    For Each fsFile In fsDir.Files

        strVCName = "C:\VCARDS\" & fsFile.Name
        Set objOL = CreateObject("Outlook.Application")
        Set colInsp = objOL.Inspectors
            If colInsp.Count = 0 Then
            Set objWSHShell = CreateObject("WScript.Shell")
            objWSHShell.Run Chr(34) & strVCName & Chr(34)
            Set colInsp = objOL.Inspectors
        If Err = 0 Then
                Do Until colInsp.Count = 1
                    DoEvents
                Loop
                colInsp.Item(1).CurrentItem.Save
                colInsp.Item(1).Close olDiscard
                Set colInsp = Nothing
                Set objOL = Nothing
                Set objWSHShell = Nothing
            End If
        End If

    Next

    End Sub

  6. Finally, run the macro to automatically import and save all the individual files into Outlook.

But before running the macro just ensure the following:

  1. Whether Outlook is the default program to open *.vcf files. You can check this by right-clicking the file and choosing ‘Properties’. The Properties Dialog Box will open. Go to the General Tab and check whether Opens With has Microsoft Office Outlook written besides it. If it’s not so, then click on the Change button and select ‘Microsoft Office Outlook’ from the list of programs. In the ‘Properties Dialog Box’ click Apply and OK.
  2. Check Macro Security Settings in MS Outlook. Go to Tools->Macro->Security. Set it to ‘No Security Check for Macros’; run the macro and after finishing reset your previous Macro Setting.
  3. (For my use only) Delete all previously saved contacts in Outlook before running the macro.

And do the following after macro finishes the import:

  1. Change back the default program to open *.vcf files from Outlook to the previously set default program (in my case its Notepad++).
  2. Change back the Macro Security settings from ‘No Security Check for Macros’ to your previously set one (in my case "Warning for signed macros and disable all unsigned macros”).

P.S. It will take some time to complete the operation (mine took around 3 minutes for 670 vcf files), depending upon the no. of vCards you are importing, as it opens the individual files and saves it in Outlook.
Further, you cannot do anything else on your system in the meanwhile. It works fine in Microsoft Office Outlook 2003 and 2007; not sure about others.