Hey, Scripting Guy! How Can I Save My Outlook Contacts as .MSG Files?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! I would like to export all my contacts as .MSG files. Is that possible?
— FG

SpacerHey, Scripting Guy! AnswerScript Center

Hey, FG. You know, the other day the Scripting Guy who writes this column was watching his … beloved … Seattle Mariners play the Angels. (The Scripting Guy who writes this column enjoys watching the Mariners, because they’re the only ones having a worse year than he is.) As the camera panned through the crowd, the Scripting Guy who writes this column noticed a fan sitting behind home plate, a fan dressed in a complete set of catcher’s gear: facemask, shin guards, chest protector, the works. That was something that the Scripting Guy who writes this column (and who watches a lot of baseball) had never seen before. A few years ago this same Scripting Guy sat next to a fan dressed in a gorilla suit. But a fan dressed as a catcher? That was a new one.

Note. Although he resisted as long as he could, the Scripting Guy who writes this column eventually asked the guy next to him, “Why a gorilla suit?” “I just had this overwhelming urge to wear it,” said the guy in the gorilla suit. “Sometimes you just have to do stuff like this, you know?”

As it turns out, the guy in the catcher’s gear was no ordinary fan; instead, he was (and still is, as far as we know) Johjiman Kita, a Japanese comedian. According to the Seattle Times, one of the cornerstones of Kita’s act is a killer impersonation of Mariner catcher Kenji Johjima; apparently Kita was in town to watch the Mariners and gather more material for his act. Granted, that doesn’t really explain why he had to come to the ballpark dressed in catcher’s gear. But, then again, sometimes you just have to do stuff like this, you know?

To tell you the truth, the catcher’s gear didn’t interest the Scripting Guy who writes this column all that much; after all, he’s been to football games in December where half the fans were dressed as Santa Claus and the other half were standing around shirtless in the 30-degree weather as a sign of support for the team. (Apparently football players usually don’t care much about winning, but if they look up in the stands and see some guy standing there freezing to death they get all fired up: “Come on, team; let’s go out and win one for the idiot who’s not wearing a shirt.”)

Note. In case you’re wondering, the Scripting Guy who writes his column has never gone to a football game without his shirt, nor has he ever worn a gorilla suit anywhere, let alone to a baseball game. On one occasion he did compliment the Scripting Editor for having the courage to wear a gorilla suit to work, only to find out that – well, never mind.

Instead, what interested the Scripting Guy who writes this column was the notion that someone could make their living by doing an impression of Mariner catcher Kenji Johjima. (No offense to Kenji Johjima, but ….) Needless to say, that seemed a little far-fetched. But, then again, it’s probably no more far-fetched than the idea that someone could make a living by writing a daily scripting column:

Const olFolderContacts = 10
Const olMSG = 3

Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")

Set colContacts = objNamespace.GetDefaultFolder(olFolderContacts).Items

For Each objContact in colContacts
    If TypeName(objContact) = "ContactItem" Then
        strName = objContact.FullName
        strPath = "C:\Test\" & strName & ".msg"
        objContact.SaveAs strPath, olMSG
    End If
Next

OK, let’s see if we can figure out what’s going on here. To begin with, our script starts out by defining a pair of constants: olFolderContacts (which we’ll use to point the script towards the Contacts folder), and olMSG (which defines the format we want to use when we save each contact.) After defining the two constants we create an instance of the Outlook.Application object, then use this line of code to bind to the MAPI namespace:

Set objNamespace = objOutlook.GetNamespace("MAPI")

Note. As it turns out, the MAPI namespace is the only namespace you can connect to. Nevertheless, you still have to call the GetNamespace method to explicitly connect to the MAPI namespace; if you don’t, your script will fail. Isn’t it kind of silly to have to connect to the MAPI namespace even if there is no other namespace to connect to, and even if there is no other way to write an Outlook script? Hey, sometimes you just have to do stuff like this, you know?

That brings us to this line of code:

Set colContacts = objNamespace.GetDefaultFolder(olFolderContacts).Items

What we’re doing here is connecting to the Contacts folder (note the constant oldFolderContacts) and retrieving a collection of all the Items found in that folder. And what are we going to do with the stuff we retrieve from the Contacts folder? Well, for starters, we’re going to set up a For Each loop that lets us loop through each and every item in the collection:

For Each objContact in colContacts

So now we’re ready to start saving contacts as .MSG files, right? Well, no, not just yet. When we first hammered out this script we thought we were ready to start saving contacts as .MSG files. However, we ran into a slight problem: our Contacts folder included distribution lists as well as contacts. Because FG only mentioned saving contacts as .MSG files, we decided to see if we could find a way to distinguish between contacts and distribution lists.

So could we find a way to distinguish between contacts and distribution lists? Oh, right; we almost forgot all about that. As it turns out, one easy way to do this is to use VBScript’s TypeName function to examine the object type for each item in the collection:

If TypeName(objContact) = "ContactItem" Then

Not too surprisingly, if the object type is ContactItem then we’re dealing with a contact. If the object type is anything but ContactItem then we aren’t dealing with a contact.

And what if we are dealing with a ContactItem? Well, in that case we execute this block of code:

strName = objContact.FullName
strPath = "C:\Test\" & strName & ".msg"
objContact.SaveAs strPath, olMSG

So what’s going on here? Well, to begin with, we’re grabbing the value of the contact’s FullName property and storing it in a variable named strName; that’s going to make strName equal to, well, the contact’s full name:

Ken Myer

After retrieving the contact name we then use the following line of code to construct a path for our new .MSG file:

strPath = "C:\Test\" & strName & ".msg"

In other words, we’re going to end up with a file path similar to this:

C:\Test\Ken Myer.msg

Note. What’s that? You say you don’t like the blank space in the file name? That’s fine; just don’t use the FullName property to pick a name for your .MSG file. Instead, try a different property, such as LastFirstNoSpace, a property that returns a value similar to this:

MyerKen

Once we have a file path we can save the contact information simply by calling the SaveAs method, passing SaveAs the complete file path and the format (olMSG) we want to use when saving the information:

objContact.SaveAs strPath, olMSG

And then from there it’s back to the top of the loop, where we repeat the process with the next item in the collection.

That should do it, FG. As for Johjiman Kita, well, the Scripting Guy who writes this column tried searching the Internet for Johjiman Kita, but he didn’t find many hits. However, he did find a couple of pages in Japanese, and decided to give the “Translate this page” option a try. (Machine translations such as these are the wave of the future in the software world.) Here’s a sample of what turned up on the “translated” page:

“In pattern of the fourth Jojima Ken Osamu flag contention small school junior high school rubber-ball baseball championship conference of Jojima Ken Osamu formality HPThe photograph of George man north was published”

Sounds reasonable enough. He also found a discussion board that included the following conversation:

“Revival of jaw! Per today so the air does with home run!”

“It is the out of order jaw, but very such a time you will support the fan!”

You know what? You’re right: that does sound like something that’s been copied directly out of a Hey, Scripting Guy! column, doesn’t it? Hey, Scripting Editor, take that gorilla suit off and go warm up the car; we’ve got people to sue!

Sometimes you just have to do stuff like that, you know?

0 comments

Discussion is closed.

Feedback usabilla icon