VBScript to import multiple VCF (VCard) Contact Cards into Outlook 2007
My partner just got her new phone and wanted to import all of her contact numbers, of course this being electronics the actual task became my responsibility, should have seen that coming.

I installed the relevant software for each of her phones and managed to export all her contacts into VCF VCard format, which then could be imported into Outlook and sent directly to her new smartphone.

At this point I found that she had umpteen contacts and outlook only allows you to enter one at a time (typical...). I did some searching and did find a VBA Script to import multiple records, or I could pay for an application to perform the task (well, I'm not paying if I don't need too).

The VBA Script did throw up several errors when running or would constantly get itself into a muddle and loop continuously, so I decided to write my own version in much loved VBScript.

Outlook 2007 Import Multiple VCards (VCF) Code


Option Explicit

' ########################################################################################
' # VBScript   : ProcessVCards.vbs                                                       #
' # Coded By   : Matt Wakeling                                                           #
' # Coded Date : 01st November 2011                                                      #
' ########################################################################################
' # Description     : VBScript to import multiple VCF Contact Cards into Outlook 2007    #
' #                                                                                      #
' # Known Problems  : The Outlook objects are cleared every contact and a sleep          #
' #                   command has been added after processing each contact as the        #
' #                   orignal script caused errors, which would not usually occur. I     #
' #                   believe this is caused by the processing speed of the PC.          #
' #                                                                                      #
' #                   This method worked 100% of the time, so it may not be best         #
' #                   coding practice (i.e. re-declaring the objects every time) but     #
' #                   does work, which is more important :-)                             #
' ########################################################################################

' Initialise Constants
Const WAIT_SECONDS  = 2
Const VCARD_DIRECTORY  = "C:\VCARDS"

Const olContact = 40

' Initialise Variables
Dim objWSHShell
Dim objOutlook 
Dim objActiveInspector
Dim strVCFilename 
Dim objFileSystemObject 
Dim objFSODirectory
Dim objFSOFile 
Dim objItem
Dim lngReturnValue 
Dim olDiscard
    
Set objFileSystemObject = CreateObject("Scripting.FileSystemObject")       
    
Set objFSODirectory = objFileSystemObject.GetFolder(VCARD_DIRECTORY)        
    
Set objWSHShell = CreateObject("WScript.Shell")       
      

For Each objFSOFile In objFSODirectory.Files 
  If UCase(Right(objFSOFile.Name,3)) = "VCF" Then 
    strVCFilename = "C:\VCARDS\" & objFSOFile.Name
                      
    Set objOutlook = CreateObject("Outlook.Application")
    If Not(objOutlook Is Nothing) Then 
      lngReturnValue = objWSHShell.Run (Chr(34) & strVCFilename & Chr(34), 0, True)
              
      Set objActiveInspector = objOutlook.ActiveInspector
             
      Set objItem = objActiveInspector.CurrentItem
             
      If (objItem.Class = olContact) Then
        objActiveInspector.CurrentItem.Save
        objActiveInspector.CurrentItem.Close olDiscard

        WScript.echo "Successfully Imported : " & objFSOFile.Name
      End If
              
      WScript.sleep WAIT_SECONDS * 1000 
   
      Set objItem  = Nothing
      Set objActiveInspector  = Nothing
      Set objOutlook = Nothing
    Else
      WScript.echo "Outlook Connection Issue, file " & objFSOFile.Name & " not imported"
    End If
  Else
    WScript.echo objFSOFile.Name & " is not a valid Contact Card"
  End If 
Next

Set objFileSystemObject  = Nothing     
Set objFSODirectory  = Nothing 
Set objWSHShell = Nothing



To run the script perform the following tasks...
  1. OPTIONAL Clear or Move your contacts in the default contacts folder in Outlook 2007.
  2. Create a new directory in C:\ called VCARDS.
  3. Create a new directory in C:\VCARDS called VBScript.
  4. Create a new text document and paste in the above code.
  5. Save the new text document as 'ProcessVCards.vbs'.
  6. Copy all your VCard (VCF Files) contacts to the C:\VCARDS directory.
  7. Open a command prompt (START --> RUN --> CMD).
  8. Change directory to C:\VCARDS\SCRIPT.
  9. Type 'cscript ProcessVCards.vbs' and press .


When executed the VBScript should produce output similar to the following to show everything is working correctly...



The default outlook contacts screen will show output similar to the following...



I hope this is of some use to you all, please feel free to leave a comment...