{"id":55413,"date":"2008-06-06T02:00:00","date_gmt":"2008-06-06T02:00:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2008\/06\/06\/hey-scripting-guy-how-can-i-save-my-outlook-contacts-as-msg-files\/"},"modified":"2008-06-06T02:00:00","modified_gmt":"2008-06-06T02:00:00","slug":"hey-scripting-guy-how-can-i-save-my-outlook-contacts-as-msg-files","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-save-my-outlook-contacts-as-msg-files\/","title":{"rendered":"Hey, Scripting Guy! How Can I Save My Outlook Contacts as .MSG Files?"},"content":{"rendered":"<p><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\" \/> <\/p>\n<p>Hey, Scripting Guy! I would like to export all my contacts as .MSG files. Is that possible?<br \/>&#8212; FG<\/p>\n<p><img decoding=\"async\" border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\" \/><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\" \/><a href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><img decoding=\"async\" class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\" \/><\/a> <\/p>\n<p>Hey, FG. You know, the other day the Scripting Guy who writes this column was watching his \u2026 beloved \u2026 Seattle Mariners play the Angels. (The Scripting Guy who writes this column enjoys watching the Mariners, because they\u2019re 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\u2019s 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.<\/p>\n<table id=\"E1C\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td>\n<p class=\"lastInCell\"><b>Note<\/b>. Although he resisted as long as he could, the Scripting Guy who writes this column eventually asked the guy next to him, \u201cWhy a gorilla suit?\u201d \u201cI just had this overwhelming urge to wear it,\u201d said the guy in the gorilla suit. \u201cSometimes you just have to do stuff like this, you know?\u201d<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>As it turns out, the guy in the catcher\u2019s 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\u2019s 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\u2019t really explain why he had to come to the ballpark dressed in catcher\u2019s gear. But, then again, sometimes you just have to do stuff like this, you know?<\/p>\n<p>To tell you the truth, the catcher\u2019s gear didn\u2019t interest the Scripting Guy who writes this column all that much; after all, he\u2019s 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\u2019t 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: \u201cCome on, team; let\u2019s go out and win one for the idiot who\u2019s not wearing a shirt.\u201d)<\/p>\n<table id=\"EHD\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td>\n<p class=\"lastInCell\"><b>Note<\/b>. In case you\u2019re 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 <i>did<\/i> compliment the Scripting Editor for having the courage to wear a gorilla suit to work, only to find out that \u2013 well, never mind.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>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 \u2026.) Needless to say, that seemed a little far-fetched. But, then again, it\u2019s probably no more far-fetched than the idea that someone could make a living by writing a daily scripting column:<\/p>\n<pre class=\"codeSample\">Const olFolderContacts = 10\nConst olMSG = 3\n\nSet objOutlook = CreateObject(\"Outlook.Application\")\nSet objNamespace = objOutlook.GetNamespace(\"MAPI\")\n\nSet colContacts = objNamespace.GetDefaultFolder(olFolderContacts).Items\n\nFor Each objContact in colContacts\n    If TypeName(objContact) = \"ContactItem\" Then\n        strName = objContact.FullName\n        strPath = \"C:\\Test\\\" &amp; strName &amp; \".msg\"\n        objContact.SaveAs strPath, olMSG\n    End If\nNext\n<\/pre>\n<p>OK, let\u2019s see if we can figure out what\u2019s going on here. To begin with, our script starts out by defining a pair of constants: olFolderContacts (which we\u2019ll 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 <b>Outlook.Application<\/b> object, then use this line of code to bind to the MAPI namespace:<\/p>\n<pre class=\"codeSample\">Set objNamespace = objOutlook.GetNamespace(\"MAPI\")<\/pre>\n<table id=\"E4D\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td>\n<p class=\"lastInCell\"><b>Note<\/b>. As it turns out, the MAPI namespace is the <i>only<\/i> namespace you can connect to. Nevertheless, you still have to call the <b>GetNamespace<\/b> method to explicitly connect to the MAPI namespace; if you don\u2019t, your script will fail. Isn\u2019t 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?<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>That brings us to this line of code:<\/p>\n<pre class=\"codeSample\">Set colContacts = objNamespace.GetDefaultFolder(olFolderContacts).Items<\/pre>\n<p>What we\u2019re doing here is connecting to the Contacts folder (note the constant oldFolderContacts) and retrieving a collection of all the <b>Items<\/b> found in that folder. And what are we going to do with the stuff we retrieve from the Contacts folder? Well, for starters, we\u2019re going to set up a For Each loop that lets us loop through each and every item in the collection:<\/p>\n<pre class=\"codeSample\">For Each objContact in colContacts<\/pre>\n<p>So now we\u2019re ready to start saving contacts as .MSG files, right? Well, no, not just yet. When we first hammered out this script we <i>thought<\/i> 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 <i>contacts<\/i> as .MSG files, we decided to see if we could find a way to distinguish between contacts and distribution lists.<\/p>\n<p>So <i>could<\/i> 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\u2019s <b>TypeName<\/b> function to examine the object type for each item in the collection:<\/p>\n<pre class=\"codeSample\">If TypeName(objContact) = \"ContactItem\" Then<\/pre>\n<p>Not too surprisingly, if the object type is <i>ContactItem<\/i> then we\u2019re dealing with a contact. If the object type is anything <i>but<\/i> ContactItem then we <i>aren\u2019t<\/i> dealing with a contact. <\/p>\n<p>And what if we <i>are<\/i> dealing with a ContactItem? Well, in that case we execute this block of code:<\/p>\n<pre class=\"codeSample\">strName = objContact.FullName\nstrPath = \"C:\\Test\\\" &amp; strName &amp; \".msg\"\nobjContact.SaveAs strPath, olMSG\n<\/pre>\n<p>So what\u2019s going on here? Well, to begin with, we\u2019re grabbing the value of the contact\u2019s <b>FullName<\/b> property and storing it in a variable named strName; that\u2019s going to make strName equal to, well, the contact\u2019s full name:<\/p>\n<pre class=\"codeSample\">Ken Myer<\/pre>\n<p>After retrieving the contact name we then use the following line of code to construct a path for our new .MSG file:<\/p>\n<pre class=\"codeSample\">strPath = \"C:\\Test\\\" &amp; strName &amp; \".msg\"<\/pre>\n<p>In other words, we\u2019re going to end up with a file path similar to this:<\/p>\n<pre class=\"codeSample\">C:\\Test\\Ken Myer.msg<\/pre>\n<table id=\"ECG\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td>\n<p><b>Note<\/b>. What\u2019s that? You say you don\u2019t like the blank space in the file name? That\u2019s fine; just don\u2019t use the FullName property to pick a name for your .MSG file. Instead, try a different property, such as <b>LastFirstNoSpace<\/b>, a property that returns a value similar to this:<\/p>\n<pre class=\"codeSample\">MyerKen<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>Once we have a file path we can save the contact information simply by calling the <b>SaveAs<\/b> method, passing SaveAs the complete file path and the format (olMSG) we want to use when saving the information:<\/p>\n<pre class=\"codeSample\">objContact.SaveAs strPath, olMSG<\/pre>\n<p>And then from there it\u2019s back to the top of the loop, where we repeat the process with the next item in the collection.<\/p>\n<p>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\u2019t find many hits. However, he did find a couple of pages in Japanese, and decided to give the \u201cTranslate this page\u201d option a try. (Machine translations such as these are the wave of the future in the software world.) Here\u2019s a sample of what turned up on the \u201ctranslated\u201d page:<\/p>\n<p>\u201cIn 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\u201d<\/p>\n<p>Sounds reasonable enough. He also found a discussion board that included the following conversation:<\/p>\n<p>\u201cRevival of jaw! Per today so the air does with home run!\u201d<\/p>\n<p>\u201cIt is the out of order jaw, but very such a time you will support the fan!\u201d<\/p>\n<p>You know what? You\u2019re right: that <i>does<\/i> sound like something that\u2019s been copied directly out of a <i>Hey, Scripting Guy! <\/i>column, doesn\u2019t it? Hey, Scripting Editor, take that gorilla suit off and go warm up the car; we\u2019ve got people to sue!<\/p>\n<p>Sometimes you just have to do stuff like that, you know?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I would like to export all my contacts as .MSG files. Is that possible?&#8212; FG Hey, FG. You know, the other day the Scripting Guy who writes this column was watching his \u2026 beloved \u2026 Seattle Mariners play the Angels. (The Scripting Guy who writes this column enjoys watching the Mariners, because [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[212,49,3,5],"class_list":["post-55413","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-microsoft-outlook","tag-office","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I would like to export all my contacts as .MSG files. Is that possible?&#8212; FG Hey, FG. You know, the other day the Scripting Guy who writes this column was watching his \u2026 beloved \u2026 Seattle Mariners play the Angels. (The Scripting Guy who writes this column enjoys watching the Mariners, because [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/55413","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=55413"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/55413\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=55413"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=55413"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=55413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}