How Can I Convert an Outlook Email Message into a Text File?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I convert an Outlook email message into a text file?

— MW

SpacerHey, Scripting Guy! AnswerScript Center

Hey, MW. You know what drives the Scripting Guys crazy? (We mean besides the other Scripting Guys.) It’s this: people who barely say hello before they start making excuses:

“I’m sorry I did such a lousy job on this, but ….”

“I realize that I was supposed to have this finished today, but ….”

“I know I should have stopped to pull you out of that burning building, but ….”

The Scripting Guys don’t believe in excuses: either you can do something or you can’t. Period.

On the other hand, the Scripting Guys do believe in caveats. And that’s exactly what you’re 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, but you have to realize that we’ll 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’d 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’t be able to show you that in today’s column; if you’d like to know how to locate specific messages take a look at the Office Space column Filtering Email Messages in Microsoft Outlook.

Another excuse – um, another caveat is that we can’t give you a fully-automated solution here; that’s 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 Yes in that dialog box. (And it must be an actual click; you can’t use SendKeys to emulate a mouse click.) Your script will work, but (there’s that word again) you can’t just schedule the script to run by itself; you’ll need to be there when the script runs so you can take care of the dialog box.

As always seems to be the case, the … caveats … take up way more space than the script. Here’s a script that saves the first message in your Inbox to a file named C:\Scripts\MailMessage.txt:

Const olFolderInbox = 6
Const olTxt = 0

Set objOutlook = CreateObject(“Outlook.Application”) Set objNamespace = objOutlook.GetNamespace(“MAPI”) Set objFolder = objNamespace.GetDefaultFolder(olFolderInbox)

Set colMailItems = objFolder.Items

Set objItem = colMailItems.GetLast() objItem.SaveAs “C:\Scripts\MailMessage.txt”, olTxt

We begin by defining a pair of constants: olFolderInbox (with a value of 6), which we’ll 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 GetDefaultFolder method):

Set objOutlook = CreateObject(“Outlook.Application”)
Set objNamespace = objOutlook.GetNamespace(“MAPI”)
Set objFolder = objNamespace.GetDefaultFolder(olFolderInbox)

After we’ve 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:

Set colMailItems =  objFolder.Items

Once we have that collection in hand we use the GetLast() method to bind to the first item in the collection (that is, the first mail message in the Inbox). We then call the SaveAs 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.

Simple, straightforward, and, best of all, no excuses. Just the way we Scripting Guys like it.

0 comments

Discussion is closed.

Feedback usabilla icon