Click here to Skip to main content
15,884,388 members
Articles / Productivity Apps and Services / Microsoft Office
Tip/Trick

Export MS-Outlook Messages as EML or MS-Word Files With Attachments as Separate Files

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
16 Sep 2016CPOL2 min read 24.5K   423   4   6
Take this ready-to-use VBA module for MS-Outlook, that lets you export all mail messages selected in the current Outlook email browser to the file system.

Introduction

Outlooks lets you manually save all attachments of a mail message to the file system, but not the pure text of the message. This VBA macro saves all attachments and converts the text into an MS-Word document.

Outlook doesn't offer an export of the pure Mime content of a mail message to the file system in EML format (i.e., MIME RFC 822 standard format). This VBA macro in connection with a registry setting lets you export messages in EML format, e.g., as an export file for other mail software like Thunderbird.

Background

There are many solutions out there which let you export attachments of mail messages, so I won't comment on that. I wanted the text be saved as a docx Word document instead of a msg file which contains all the attachments as well, that's all.

For exporting EML files, I only found commercial add-ins without source code, but no VBA code. I wonder why, because the solution introduced in this tip is very simple.

Using the Code

You simply copy the VBA module into your Outlook application and you'll be able to run the 2 macros "ExportAsWordAndAttachments" and "ExportAsEml". Remember to enable macros and to enable the macro ribbon in your Outlook settings.

With the default settings, Outlooks only shows the message headers. So in the first place, just convince Outlook to save the complete message source by modifying the registry as follows:

[HKEY_CURRENT_USER\SOFTWARE\Microsoft\Office\15.0\Outlook\Options\Mail] 
"SaveAllMIMENotJustHeaders"=dword:00000001 

This works for all Outlook versions (2007, 2010, 2013). The sample above is for Outlook 2013. For other versions, just replace "15" with your Outlook version number. Please refer to this article for details. After a re-start of Outlook, all (and only) new mail messages will be stored with all Mime content.

With this setting in place, the following tip I found on the internet will not only retrieve the message headers, but the complete Mime content. This is true for all inbound messages. Here is the key VBA function we need:

Private Function GetInetHeaders(olkMsg As Variant) As String
   Const PR_TRANSPORT_MESSAGE_HEADERS = "http://schemas.microsoft.com/mapi/proptag/0x007D001E"
   Dim olkPA As Outlook.PropertyAccessor
   Set olkPA = olkMsg.PropertyAccessor
   GetInetHeaders = olkPA.GetProperty(PR_TRANSPORT_MESSAGE_HEADERS)
   Set olkPA = Nothing
End Function

Now registry setting and VBA glued together let you export the message as an EML file, which must be saved as ANSI, not as Unicode. Here's the code snippet:

sFullPath = "whatever path you want.EML"
Set fileSystem = CreateObject("Scripting.FileSystemObject")
Set txtStream = fileSystem.CreateTextFile(sFullPath, False, False)
rawContent = GetInetHeaders(olkMsg)
txtStream.WriteLine (rawContent)
txtStream.Close

Going through the VBA code, you'll notice that when selecting the target folder for the export, I used FileDialog(msoFileDialogFolderPicker) which is unfortunately not implemented in Outlook.Application. but it is in Word.Application. So I used this one for simplicity, not for beauty. That's why you'll have to reference Microsoft Word xx.x Object Library (xx.x = 15.0 for Office 2013).

Everything else in the VBA module is quite self-explanatory even for beginners. The zip file attached contains a version for Microsoft Office 2013.

Points of interest

As a C# developer, you may be interested in "Reading an Outlook MSG File in C#". Very nice.

History

  • 1.0 - Initial release
  • 1.01 - Added missing download file

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
BugError on Office Proplus Pin
Member 1556991717-Mar-22 5:50
Member 1556991717-Mar-22 5:50 
Bugeml file created but message content missing Pin
RJ_18-Aug-20 8:08
RJ_18-Aug-20 8:08 
QuestionHi I am interested but can't see the code? BR Richard Pin
RClark29-Nov-15 14:18
RClark29-Nov-15 14:18 
AnswerRe: Hi I am interested but can't see the code? BR Richard Pin
Herbert Hopfig28-Feb-16 6:09
Herbert Hopfig28-Feb-16 6:09 
GeneralMessage Closed Pin
17-Sep-16 0:42
Pia Raichand17-Sep-16 0:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.