Save email message as text file

Last reviewed on September 20, 2013   —  14 comments

A visitor to OutlookForums saves messages as text files and was tired of changing the default Save as format to txt.

I save multiple messages everyday as text files. I just upgraded from 2007 outlook to 2010. Before the upgrade I had it defaulted to save as text. I did not do this that I recall it just has been that way. Now that I have upgraded it is defaulted to msg. Please tell me if there is a way to do this as it will save me an immense amount of excess clicking.

This macro is a manual version of E-Mail: Save new items immediately as files. Unlike the original macro, which saves all new messages as text file, you need to select a message and run this macro to save it as a text file.

For other options and utilities, see How to Save Email in Windows File System.

Save selected message as a text file

A version of this macro which saves all selected messages as multiple individual text files is at SaveSelectedMailAsTxtFile. The code sample at SaveSelectedMailBodiesTxtFiles is the modification discussed in this comment and reply.

 Sub SaveMailAsFile()
 Const OLTXT = 0
 Dim oMail As Outlook.mailItem
 Dim sPath As String
  Dim dtDate As Date
  Dim sName As String

  Set oMail = Application.ActiveExplorer.Selection.Item(1)
  sName = oMail.Subject
  ReplaceCharsForFileName sName, "_"

  dtDate = oMail.ReceivedTime
  sName = Format(dtDate, "yyyymmdd", vbUseSystemDayOfWeek, _
    vbUseSystem) & Format(dtDate, "-hhnnss", _
    vbUseSystemDayOfWeek, vbUseSystem) & "-" & sName & ".txt"

  oMail.SaveAs "C:\path\to\save\" & sName, OLTXT
End Sub

Private Sub ReplaceCharsForFileName(sName As String, _
  sChr As String _
  sName = Replace(sName, "/", sChr)
  sName = Replace(sName, "\", sChr)
  sName = Replace(sName, ":", sChr)
  sName = Replace(sName, "?", sChr)
  sName = Replace(sName, Chr(34), sChr)
  sName = Replace(sName, "<", sChr)
  sName = Replace(sName, ">", sChr)
  sName = Replace(sName, "|", sChr)
End Sub

Save selected messages to a single text file

This code sample saves the selected messages in one text file, replicating Outlook's behavior when you select multiple messages and choose Save as. It uses the current date and folder name as the file name and saves it to the user's My Documents folder.

Sub MergeSelectedEmailsIntoTextFile()

Dim objFS As New Scripting.FileSystemObject, objFile As Scripting.TextStream
Dim objItem As Object, strFile As String
Dim Folder As Folder
Dim sName As String

' Use your User folder as the initial path
Dim enviro As String
enviro = CStr(Environ("USERPROFILE"))

  If ActiveExplorer.Selection.Count = 0 Then Exit Sub
' use the folder name in the filename
  Set Folder = Application.ActiveExplorer.CurrentFolder

' add the current date to the filename
sName = Format(Now(), "yyyy-mm-dd")

' The folder pathyou use needs to exist
strFile = enviro & "\Documents\" & sName & "-" & Folder & ".txt"
  Set objFile = objFS.CreateTextFile(strFile, False)
  If objFile Is Nothing Then
    MsgBox "Error creating file '" & strFile & "'.", vbOKOnly + vbExclamation _
      , "Invalid File"
    Exit Sub
  End If
  For Each objItem In ActiveExplorer.Selection
  With objFile
    .Write vbCrLf & "--Start--" & vbCrLf
    .Write "Sender: " & objItem.Sender & " <" & objItem.SenderEmailAddress & ">" & vbCrLf
    .Write "Recipients : " & objItem.To & vbCrLf
    .Write "Received: " & objItem.ReceivedTime & vbCrLf
    .Write "Subject: " & objItem.Subject & vbCrLf & vbCrLf
    .Write objItem.Body
    .Write vbCrLf & "--End--" & vbCrLf
 End With

  MsgBox "Email text extraction completed!", vbOKOnly + vbInformation, "DONE!"
  Set objFS = Nothing
  Set objFile = Nothing
  Set objItem = Nothing
End Sub

Replace the code between strFile = enviro... and objFile.close with the following. To add more fields, add more objFile.Write lines.

strFile = enviro & "\Documents\" & sName & "-" & Folder & ".txt"
Set objFile = objFS.OpenTextFile(strFile, ForAppending, True)
       For Each objItem In ActiveExplorer.Selection
        objFile.Write (objItem.Body)

Super short code

This code is super short and works on the currently open or selected message only. You'll need to GetCurrentItem function to use this macro. You'll need to add a check mark to the Microsoft Scripting Runtime in Tools, References.

Messages are appended to one file.

Public Sub SaveEmailBody()
Dim objMail As MailItem
    Dim fso As New FileSystemObject
    Dim ts As TextStream
' get the function at
    Set objMail = GetCurrentItem()
    Set ts = fso.OpenTextFile("E:\Documents\mailfile.txt", ForAppending, True)

    ts.Write (objMail.Body)
    Set ts = Nothing
    Set fso = Nothing

End Sub

Written by

Diane Poremsky
A Microsoft Outlook Most Valuable Professional (MVP) since 1999, Diane is the author of several books, including Outlook 2013 Absolute Beginners Book. She also created video training CDs and online training classes for Microsoft Outlook. You can find her helping people online in Outlook Forums as well as in the Microsoft Answers and TechNet forums.

Please post long or more complicated questions at Outlookforums.

14 responses to “Save email message as text file”

  1. danni

    I'm most interested in the first of these. I managed to get it to work in Outlook 2010, but not in 2013. Is there anything that might be different for 2013?

  2. Ramesh Govindarajan

    Hi there Diane,

    Thank you for the script. I am able to use this - - in Outlook 2010 successfully.

    One question though - this script basically extracts the entire email. Is there a way to just get the body out of the email and none of the header or subject information?

    Thank you for your assistance!

  3. Ramesh Govindarajan

    Thank you. I'm no good at coding, so I understand vaguely what you're saying, but have no clue how to do it.

    So, I appreciate you adding the code here.

    Thanks again.

  4. Ramesh Govindarajan

    Hi Diane,

    I'm using this code though -

    This does not separate them out, so I'm not sure how to extract just the body from the email. All it says is -
    oMail.SaveAs "C:\Users\Diane\Dropbox\For EMO\" & sName, OLTXT

    The reason I like this is because each email goes to a separate text file.

    Thank you.

  5. Ramesh Govindarajan

    Wow, Ma'am. You are amazing. Thank you so much!!

    Worked like a champ.

Leave a Reply

If the Post Coment button disappears, press your Tab key.