Hey, Scripting Guy! How can I save all my contacts as VCards?
— ET
Hey, ET. You know, we have to tell you the truth: after nearly 800 Hey, Scripting Guy! articles the Scripting Guy who writes this column is beginning to get a little burnt-out. Fortunately, the Scripting Editor has often volunteered to do the column for him, and the Scripting Guy who writes this column has decided to take her up on that generous offer. Therefore, and without any further adieu, let’s turn today’s Hey, Scripting Guy! over to the Scripting Editor:
Hey, ET. Oh, ET: I get it; that was, like, the name of that Martian guy, right? That’s, like, really funny, you know, I mean, I can’t, like, wait to tell Cassandra. Um, so, anyway, ET (tee-hee!), you know, it sounds like you want to save your contacts as …something … oh, wait, I know: you want to save your contacts as, like, VCards, right?. So, like, do you have any idea what VCards are, you know, I mean, are they, like, better than contacts, so, like, maybe that’s why you want to save your contacts as those card thingies? I mean, because I really like contacts, you know, like, my friend, Jessica, she has these contacts that can, like, make her eyes look like different colors, you know, like, on Monday, you know, maybe she has, like blue eyes, and then, like, on Tuesday or whatever she changes contacts and, like, suddenly it looks like she has green eyes, and, like, that is way cool, you know, and like Jessica’s boyfriend, Todd, Todd’s like, “Wow, it’s like I have a different girlfriend every day of the week!” and I’m thinking, I wish you did have a different girlfriend every day of the week, because Todd is a real hunk and I would totally go out with him except, like, Jessica’s my friend, you know, so I guess that wouldn’t be very cool, you know? So anyway, contacts are awesome but, like, maybe these VCards, whatever they are, well, like, maybe they’re better, you know? |
OK, thank you, Scripting Editor. That helped; the Scripting Guy who writes this column suddenly feels much better and much more refreshed now. In fact, we’ll tell you what: why don’t you finish doing whatever Scripting Editors do, and we’ll show ET how to – um, yes, we get it, although we don’t believe the real ET was actually a Martian. Anyway, why don’t you go do whatever it is you do and we’ll show ET a script that can save all his contacts as VCards:
On Error Resume NextConst olFolderContacts = 10 Const olVCard = 6
Set objOutlook = CreateObject(“Outlook.Application”) Set objNamespace = objOutlook.GetNamespace(“MAPI”)
Set colContacts = objNamespace.GetDefaultFolder(olFolderContacts).Items
For Each objContact In colContacts strName = objContact.FirstName & objContact.LastName strPath = “C:\Test\” & strName & “.vcf” objContact.SaveAs strpath, olVCard Next
As you can see, ET, the script – sorry; just a second. Yes, those shoes are darling, just darling. And they look totally awesome with that sweater.
Totally.
Anyway, as we were saying, the script starts out by implementing the On Error Resume Next statement. Whenever we work with contacts we’re prone to getting somewhat-inexplicable errors from time-to-time, errors that, to be honest, don’t make a lot of sense to us; for example, the script might fail, complaining that a valid contact property (like FirstName) isn’t a valid property after all. We’re not sure if this is a generic problem or if it just has something to do with our copy of Outlook; however, tossing in the On Error Resume Next statement seems to take care of the problem, so we did that here just as a precaution.
Our first real bit of coding is to define a pair of constants: olFolderContacts, which tells the script which Outlook folder we want to work with; and olVCard, which tells the script the file format to use when saving the contact information. After defining the constants we create an instance of the Outlook.Application object, then use the following line of code to bind to the MAPI namespace (a required step, even though the MAPI namespace is the only namespace we can bind to):
Set objNamespace = objOutlook.GetNamespace(“MAPI”)
Once we’ve successfully connected to Outlook we can then use the following line of code and the GetDefaultFolder method to retrieve a collection of all the items found in the Contacts folder:
Set colContacts = objNamespace.GetDefaultFolder(olFolderContacts).Items
That was, like, way easy, you know?
Sorry; we couldn’t resist.
Actually, that was way easy and, best of all, the rest of the script is just as easy. Our next chore is to set up a For Each loop to loop through the collection of contacts. As everyone knows – well, OK, as most people know – VCard is just another file format; consequently, that means that we can’t save contact information as a VCard unless we specify the complete file path for this new file. We decided to create a file name based on the contact’s first name and last name; thus we use this line of code to assign the values of the FirstName and LastName properties to a variable christened strName:
strName = objContact.FirstName & objContact.LastName
If we have a contact named Ken Myer that’s going to result in a file name that looks like this:
KenMyer
Once we have a unique file name we can then construct the complete path:
strPath = “C:\Test\” & strName & “.vcf”
Nothing too fancy here: we’re just combing the folder path C:\Test\ with the value of the variable strName and .vcf, which represents the file extension (because VCards use a .VCF file extension). From there we go ahead and create a VCard for our first contact, something we do by calling the SaveAs method, passing the file path (the variable strPath) and the constant olVCard as the two method parameters:
objContact.SaveAs strpath, olVCard
Important. Because creating a VCard requires you to access the contact’s email address this script won’t just run the moment you kick it off; instead, Outlook is going to pop up a message informing you that the script is trying to access address data and ask if you want to allow this access. You must answer “Yes” (or “Allow”) in order for the script to run. And, sadly, there’s no way – short of reconfiguring your Exchange setup – to bypass or automate the process of answering this message box. Just something to keep in mind. |
After that we loop around and repeat the process with the next contact in the collection. When all is said and done the folder C:\Test should include a bunch of files similar to these:
KenMyer.vcf JonathanHaas.vcf PilarAckerman.vcf
That should do it, ET. And now we’ll let the Scripting Editor have the final word on today’s column:
Um, like, you know? |
Thank you, Scripting Editor; we couldn’t have said it better ourselves.
Editor’s Note: Given that the Scripting Editor didn’t sound like a ditzy teenager even when she was a teenager (which really wasn’t that long ago, by the way), we’re not sure where the Scripting Guy who writes this column got the preceding text. What we are sure of is that the Scripting Guy who writes this column will be apologizing profusely for painting the Scripting Editor in such a poor light, just as soon as he’s done washing her car…and cleaning her office…and walking her dog…and bringing her donuts for breakfast every morning for the rest of her life…. |
0 comments