{"id":68403,"date":"2005-12-05T13:34:00","date_gmt":"2005-12-05T13:34:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/12\/05\/how-can-i-convert-an-outlook-email-message-into-a-text-file\/"},"modified":"2005-12-05T13:34:00","modified_gmt":"2005-12-05T13:34:00","slug":"how-can-i-convert-an-outlook-email-message-into-a-text-file","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-convert-an-outlook-email-message-into-a-text-file\/","title":{"rendered":"How Can I Convert an Outlook Email Message into a Text File?"},"content":{"rendered":"<p><IMG 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\"> \n<P>Hey, Scripting Guy! How can I convert an Outlook email message into a text file?<BR><BR>&#8212; MW<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG 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 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> \n<P>Hey, MW. You know what drives the Scripting Guys crazy? (We mean <I>besides<\/I> the other Scripting Guys.) It\u2019s this: people who barely say hello before they start making excuses:<\/P>\n<P>\u201cI\u2019m sorry I did such a lousy job on this, but \u2026.\u201d<\/P>\n<P>\u201cI realize that I was supposed to have this finished today, but \u2026.\u201d<\/P>\n<P>\u201cI know I should have stopped to pull you out of that burning building, but \u2026.\u201d<\/P>\n<P>The Scripting Guys don\u2019t believe in excuses: either you can do something or you can\u2019t. Period.<\/P>\n<P>On the other hand, the Scripting Guys <I>do<\/I> believe in caveats. And that\u2019s exactly what you\u2019re going to get right off the bat. Can we show you how to convert an Outlook email message into a text file? You bet we can, <I>but<\/I> you have to realize that we\u2019ll be able to show you only the simplest such example: converting the first item in your Inbox to a text file. What if you want to be a little more specific? For example, what if you\u2019d like to convert a message with a specific subject line or a message sent on a specific date to a text file? Unfortunately, we won\u2019t be able to show you that in today\u2019s column; if you\u2019d like to know how to locate specific messages take a look at the Office Space column <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/officetips\/aug05\/tips0818.mspx\"><B>Filtering Email Messages in Microsoft Outlook<\/B><\/A>.<\/P>\n<P>Another excuse &#8211; um, another <I>caveat<\/I> is that we can\u2019t give you a fully-automated solution here; that\u2019s due to the security built into Outlook. In order to save an email message as a text file you need to be able to access certain properties (such as the body of the message) that trigger a security alert in Outlook. That means that when you run the script a security warning dialog box will pop up. Before the script can continue (and before the message can be saved) you will need to click <B>Yes<\/B> in that dialog box. (And it must be an actual click; you can\u2019t use SendKeys to emulate a mouse click.) Your script will work, <I>but<\/I> (there\u2019s that word again) you can\u2019t just schedule the script to run by itself; you\u2019ll need to be there when the script runs so you can take care of the dialog box.<\/P>\n<P>As always seems to be the case, the \u2026 caveats \u2026 take up way more space than the script. Here\u2019s a script that saves the first message in your Inbox to a file named C:\\Scripts\\MailMessage.txt:<\/P><PRE class=\"codeSample\">Const olFolderInbox = 6\nConst olTxt = 0<\/p>\n<p>Set objOutlook = CreateObject(&#8220;Outlook.Application&#8221;)\nSet objNamespace = objOutlook.GetNamespace(&#8220;MAPI&#8221;)\nSet objFolder = objNamespace.GetDefaultFolder(olFolderInbox)<\/p>\n<p>Set colMailItems =  objFolder.Items<\/p>\n<p>Set objItem = colMailItems.GetLast()\nobjItem.SaveAs &#8220;C:\\Scripts\\MailMessage.txt&#8221;, olTxt\n<\/PRE>\n<P>We begin by defining a pair of constants: olFolderInbox (with a value of 6), which we\u2019ll use to indicate the Outlook folder we want to connect to; and olTxt (with a value of 0) that tells Outlook in which format to save our mail message. We then have three lines of code that connect us to Outlook and the MAPI namespace and bind us to the Inbox folder (using the <B>GetDefaultFolder<\/B> method):<\/P><PRE class=\"codeSample\">Set objOutlook = CreateObject(&#8220;Outlook.Application&#8221;)\nSet objNamespace = objOutlook.GetNamespace(&#8220;MAPI&#8221;)\nSet objFolder = objNamespace.GetDefaultFolder(olFolderInbox)\n<\/PRE>\n<P>After we\u2019ve made the connection to the Inbox the rest is easy. We use this line of code to return a collection of all the items found in the Inbox:<\/P><PRE class=\"codeSample\">Set colMailItems =  objFolder.Items\n<\/PRE>\n<P>Once we have that collection in hand we use the <B>GetLast()<\/B> method to bind to the first item in the collection (that is, the first mail message in the Inbox). We then call the <B>SaveAs<\/B> method to save the message, passing SaveAs a pair of parameters: the complete path for the text file (C:\\Scripts\\MailMessage.txt) and the constant olTxt. <\/P>\n<P>Simple, straightforward, and, best of all, no excuses. Just the way we Scripting Guys like it.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I convert an Outlook email message into a text file?&#8212; MW Hey, MW. You know what drives the Scripting Guys crazy? (We mean besides the other Scripting Guys.) It\u2019s this: people who barely say hello before they start making excuses: \u201cI\u2019m sorry I did such a lousy job on this, [&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-68403","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! How can I convert an Outlook email message into a text file?&#8212; MW Hey, MW. You know what drives the Scripting Guys crazy? (We mean besides the other Scripting Guys.) It\u2019s this: people who barely say hello before they start making excuses: \u201cI\u2019m sorry I did such a lousy job on this, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68403","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=68403"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68403\/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=68403"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68403"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68403"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}