Thursday, May 10, 2007

Export all Outlook contact pictures to the File System

Somebody asked a question today about how do you export all of your Outlook contact pictures to a folder in the file system. With a little CDO 1.2 this is pretty simple let’s start with some code that logs onto the mailbox and server you specify as command line parameter and then gets the contacts folder

Public Const CdoDefaultFolderContacts = 5
snServername = wscript.arguments(0)
mbMailboxName = wscript.arguments(1)
set csCDOSession = CreateObject("MAPI.Session")
pfProfile = snServername & vbLf & mbMailboxName
csCDOSession.Logon "","",False,True,0,True, pfProfile
set cfContactsFolder = csCDOSession.getdefaultfolder(CdoDefaultFolderContacts)

When a contact in Outlook has a picture the following MAPI named property will be set to true {00062004-0000-0000-C000-000000000046}0x8015 . So in CDO 1.2 you can create a filter that will filter the collection of items in the contact folder so only those with a picture will be returned. Eg

set cfContactscol = cfContactsFolder.messages
set ofConFilter = cfContactscol.Filter
Set cfContFltFld1 = ofConFilter.Fields.Add("0x8015",vbBoolean,true,"0420060000000000C000000000000046")

Then all that’s left is to iterate though the contacts and their attachments and download the attachment that relates to the contact picture which will always be named ContactPicture.jpg. The code that downloads the contact picture includes some functions to make sure the filename is unique by using the subject of the contact (which is generally the file as property).

For Each ctContact In cfContactscol
Set collAttachments = ctContact.Attachments
For Each atAttachment In collAttachments
If = "ContactPicture.jpg" Then
fname = replace(replace(replace(replace(replace((ctContact.subject & "-" &,":","-"),"\",""),"/",""),"?",""),chr(34),"")
fname = replace(replace(replace(replace(replace(replace(fname,"<",""),">",""),chr(11),""),"*",""),"|",""),"(","")
fname = replace(replace(replace(fname,")",""),chr(12),""),chr(15),"")
atAttachment.WriteToFile("c:\contactpictures\" & fname)
wscript.echo "Exported Picture to : " & fname
End if

I put a downloadable copy of the full script here


Anonymous said...

Is it possible to replicate between an MS SQL 2005 database table containing company contact information and an MS Exchange public folder contacts folder? Our development team is creating an in house application that needs to extract or update contact information stored in a contacts public folder in our MS Exchange environment. All data must be stored centrally on an SQL 2005 database and the users should be able to see contact data both in the new app via the SQL database and also in our Public folder structure. I’ve seen a few articles saying that this can be done but nothing saying how.

Glen said...

Depends which version of Exchange you are using there was some information published recently about using EWS and SQL 2005 on Exchange 2007 although public folders are not currently supported in EWS (maybe SP1). Otherwise there are no native method for doing replication. But are you talking replication in that the Exchange or SQL side is read only and changes are just copied in or Synchronization which is quite complex if its both ways. There are things like WebDAV replication that can help when doing Sync but this is only from a Exchange perspective. A few people have written extended Stored Procedures that allows you to access a Exchange server via Mapi directly from a SQL Box eg


Joseph said...

Hi Glen, great stuff! Here's an iPhone related question. When it is synchronised with outlook/exchange (I'm using the 2007 version of both), it stores a beatiful 320x320 picture of the contact. The problem is, the file is called 'ContactPhoto' with no extension. It displays well in Outlook, but not in Outlook Web Access.

I can imagine a similar script which would scan all of the contacts and if it has the filename 'ContactPhoto' renames it to 'ContactPicture.jpg' in the correct hidden format. What do you think?

I've used one of your scripts before to do this the other way (add a 320x320 picture to the contact as a hidden ContactPicture.jpg with OutlookSpy, then push it to the iPhone) and it works great. But this would be so much easier.

Thanks in advance! Joseph

Glen said...

You wouldn't be able to rename the attachment what you would have to do is download it, delete it and then re upload it. Theoretically it should work.


gambuzino said...

what if i want to do the inverse action?
I have photos in a folder and want them to update contacts, replacing or inserting photos.

Best regards

Glen said...

There's details of how to add photos to contact via script


Anonymous said...

I am totally new to this - can´t even figure out how to run this script. can you give me a short how-to run the script. thanks

Anonymous said...

I keep getting a script error on line 2, character 1

Glen said...

You need to run the script with the name of the server and mailbox you want to export from as commandline parameters.


cscript scriptname.vbs servername mailboxname


Anonymous said...

Cheers mate, you just saved me an awful lot of time!

Tonio said...
This comment has been removed by the author.
Tonio said...

Guys, sorry for necroposting, but I need you help in minor script modification.

Is it possible somehow to export pictures in the following format:

Name Surname-Company name.jpg

instead of current script format:

Name Surname-ContactPicture.jpg?

I tried to call ctContact.CompanyName MAPI field, but script returns error. Basically, I tried other fields as well, but only ctContact.subject gives me the result.