VB Script to Export Outlook Contacts to vCard (vcf) file
12/29/08
VB Script to Export Outlook Contacts to vCard (vcf) file
Have you ever needed to export your Outlook contacts to vCard file (vcf) format? I did and I couldn't find an easy way to do it. This script takes one command line argument, the path where the vCard files will be stored. It then opens your Outlook contacts and exports all of them. Each vCard file will be named based on the first and last name of the contact.
This script only works if you run it from a command prompt using cscript. The following is an example of executing this script.
C:\scripts>cscript exportContacts.vbs /p:C:\scripts\outlookContacts
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.David.vcf
Lee.vcf
Joe.vcf
Stacy.vcf
Dan.vcf
Jason.vcf
This sample script uses the named command line arguments to pass the domain information to script. This is the domain that will be checked for type, native or mixed mode.
'==========================================================
'== VB Script to Export Outlook Contacts to vCard (vcf) files
'==
'== Copyright © 2008, Dave Moats
'==
'== This sample is provided 'AS-IS', without any
'== express or implied warranty. In no event will the
'== authors be held liable for any damages arising from
'== the use of this sample code.
'==
'== Permission is granted to anyone to use this sample
'== code for any purpose, including commercial applications,
'== subject to the following restrictions:
'==
'== The origin of this code must not be misrepresented;
'== you must not claim that you wrote the original code.
'==
'== If you use this code, an acknowledgment in the
'== documentation is requested - shown below:
'==
'== Portions Copyright © 2008,
'== Dave Moats (http://www.davemoats.com/).
'==
'=========================================================='==========================================================
'== NOTE: watch for wrapped lines and html special
'== characters in the web representation of this
'== sample code
'=========================================================='==========================================================
'==
'== exportContacts.vbs - a script used to export contacts
'== from outlook and save them in
'== vcf format
'==========================================================
option explicit'==========================================================
'== declare the local variables to be used
'==========================================================
dim scriptName, namedArgs, folderPath'==========================================================
'== get the name of the running script
'==========================================================
scriptName = wscript.scriptname'==========================================================
'== get the named command line arguments
'==========================================================
set namedArgs = wscript.arguments.namedif not namedArgs.exists("p") then
wscript.echo "Usage: " & scriptName & " /p:<output folder path> is required"
wscript.echo "Example: cscript " & scriptName & " /p:c:\path to the folder"
wscript.quit
else
folderPath = namedArgs.item("p")
end ifset namedArgs = nothing
'==========================================================
'== now call the subroutine that does all the work
'==========================================================
exportToVCF folderPath, ".vcf"wscript.quit
'==========================================================
'== sub exportToVCF - subroutine that connects to outlook
'== and exports all contacts to the path
'== specified in the exportPath argument
'==========================================================
sub exportToVCF(exportPath, ext)
'======================================================
'== declare the local variables
'======================================================
dim outApp, fldContacts, contactEntry, exp'======================================================
'== create the outlook object and then get the contacts
'======================================================
set outApp = createobject("Outlook.Application")
set fldContacts = outApp.getnamespace("MAPI").getdefaultfolder(10)'======================================================
'== here we are looping the entries in the contacts
'== folder looking for contacts - when a contact is
'== found it will be exported
'======================================================
for each contactEntry in fldContacts.items
if typename(contactEntry) = "ContactItem" thendim tmpString, tmpArr, periodLocation, outPath
tmpString = contactEntry.lastnameandfirstname
'================================================
'== creating the name of the vcf file based on
'== the first and last name fields of the contact
'================================================
tmpString = replace( tmpString, "'", "" )
tmpString = replace( tmpString, "<", "" )
tmpString = replace( tmpString, ">", "" )
tmpString = replace( tmpString, ":", "" )
tmpString = replace( tmpString, """", "" )
tmpString = replace( tmpString, "/", "" )
tmpString = replace( tmpString, "\", "" )
tmpString = replace( tmpString, "|", "" )
tmpString = replace( tmpString, "?", "" )
tmpString = replace( tmpString, "*", "" )tmpArr = split( tmpString, "," )
if ubound( tmpArr ) <> 1 then
wscript.echo tmpString & ext
outPath = exportPath & "\" & tmpString & ext
else
if tmpArr(0) = "com" then
wscript.echo trim(tmpArr(1)) & trim(tmpArr(0)) & ext
outPath = exportPath & "\" & trim(tmpArr(1)) & trim(tmpArr(0)) & ext
else
wscript.echo trim(tmpArr(1)) & " " & trim(tmpArr(0)) & ext
outPath = exportPath & "\" & trim(tmpArr(1)) & " " & trim(tmpArr(0)) & ext
end if
end if
contactEntry.saveas outPath, 6end if
next
'======================================================
'== dumping the object references that were created
'======================================================
set fldContacts = nothing
set outApp = nothingend sub
Pay close attention to the lines wrapping in this sample, the script does not have any multi-line statements.
I hope this post helped out. If it didn't, I am always looking for new scripts to add so submit a request for your question or need and I will see if I can answer it.
Dave
Trackback address for this post:
http://davemoats.com/blog/htsrv/trackback.php/176
Comments, Trackbacks, Pingbacks:
I was stupid initially in copying the 'C:\'
of
C:\scripts>cscript exportContacts.vbs
on to my command prompt window.
Finally figured it out.
As always, if you are looking for something specific and you don't see it here, drop me a note and I will see what I can do.
Cheers,
Dave
I was looking for a script what extracts all the email address from a user outlook contacts into a txt file.
Thanks
Have a great day.
Dave
Kudos to Eric for finding that for me.
Cheers,
Dave
I found, though, that tmpString ended up being empty sometimes. This means that you get the file '.vcf' created, and overwritten if multiple contacts have the same error.
A crude fix is to increment a counter for each contact, and test if tmpString is blank. If so, write the contact out under the counter + .vcf. True, it means you end up with a few files with numeric names, but at least you don't overwrite the contacts.
This post has 7 feedbacks awaiting moderation...
Leave a comment:
Windows Scripts
This is somewhere I can post interesting snippets as I come across them. Hopefully some folks out there will find this helpful.
Search
Follow Me:
Categories
- All
- Windows Scripting (37)
- Batch Scripts (13)
- Wsh Scripts (23)
Archives
- March 2009 (2)
- February 2009 (3)
- January 2009 (2)
- December 2008 (4)
- November 2008 (1)
- October 2008 (1)
- February 2008 (1)
- December 2007 (3)
- July 2007 (1)
- April 2007 (1)
- February 2007 (2)
- December 2006 (5)
- More...
Misc
Who's Online?
- Guest Users: 1




