Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
17 Sep 2016 1  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here