VB Script to Export Outlook Contact Email Addresses to a text file
02/09/09
VB Script to Export Outlook Contact Email Addresses to a text file
Have you ever needed to export the email addresses of your Outlook contacts to a text file? One of the visitors here was needing to do this. So I modified the script I wrote that exports contacts to vcf files to create a text file that just contains the email addresses of all of the contacts. This script takes one command line argument, the path where the test file will be stored. It then opens your Outlook contacts and extracts the email addresses for each contact and writes those addresses to a file named emailaddress.txt
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.
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.
'==========================================================
'== Extract Contact Email Addresses Script Sample
'==
'== Copyright © 2009, Dave Moats
'==
'== This sample is provided <b>'AS-IS'</b>, 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 © 2009,
'== 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
'==========================================================
exportAddresses folderPathwscript.quit
'==========================================================
'== sub exportAddresses - subroutine that connects to outlook
'== and exports all contacts email addresses
'== to a file in the folder specified by the exportPath argument
'==========================================================
sub exportAddresses(exportPath)'======================================================
'== declare the local variables
'======================================================
dim outApp, fldContacts, contactEntry, fso, outFile'======================================================
'== 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
'======================================================
set fso = createobject( "Scripting.FileSystemObject" )
set outFile = fso.createtextfile( exportPath & "\emailaddreses.txt", 8 )for each contactEntry in fldContacts.items
if typename(contactEntry) = "ContactItem" thendim tmpString1, tmpString2, tmpString3, outMsg
tmpString1 = contactEntry.email1address
tmpString2 = contactEntry.email2address
tmpString3 = contactEntry.email3addressif tmpString1 <> "" then
outMsg = tmpString1
end ifif tmpString2 <> "" then
outMsg = outMsg & vbcrlf & tmpString2
end ifif tmpString3 <> "" then
outMsg = outMsg & vbcrlf & tmpString3
end ifoutFile.writeline outMsg
end if
nextoutFile.close
set outFile = nothing
set fso = nothing
'======================================================
'== 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
Pingbacks:
No Pingbacks for this post yet...
This post has 42 feedbacks awaiting moderation...
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
- Web Technologies (2)
- PHP (1)
- Windows Scripting (37)
- Batch Scripts (13)
- Wsh Scripts (23)
Archives
- December 2009 (2)
- 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)
- More...
Misc
Who's Online?
- Guest Users: 2




