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