Save selected email message as .msg file

Last reviewed on December 30, 2013

This code sample will save one or more selected messages to your My Documents folder as individual .msg files. The file name includes the received date and time. Spaces and invalid characters are replaced with underscores.

Save msg

A variation of this macro that saves as a text file is at Save email message as text file. Included is a version that saves selected messages as one text file. For more information on saving to other formats, see How to Save Email in Windows File System.

See How to use the VBA Editor if you don't know how to use macros or the VBA Editor.

Option Explicit
Public Sub SaveMessageAsMsg()
  Dim oMail As Outlook.MailItem
  Dim objItem As Object
  Dim sPath As String
  Dim dtDate As Date
  Dim sName As String
  Dim enviro As String

    enviro = CStr(Environ("USERPROFILE"))
   For Each objItem In ActiveExplorer.Selection
    Set oMail = objItem
  sName = oMail.Subject
  ReplaceCharsForFileName sName, "_"

  dtDate = oMail.ReceivedTime
  sName = Format(dtDate, "yyyymmdd", vbUseSystemDayOfWeek, _
    vbUseSystem) & Format(dtDate, "-hhnnss", _
    vbUseSystemDayOfWeek, vbUseSystem) & "-" & sName & ".msg"
    sPath = enviro & "\Documents\"
  Debug.Print sPath & sName
  oMail.SaveAs sPath & sName, olMSG
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

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.

35 responses to “Save selected email message as .msg file”

  1. Maya Headley

    This is an excellent macro and it runs perfectly from VBA when I press F5, but not from Outlook itself. Can you help me troubleshoot? I want to have a button on my ribbon that I could easily press to make changes. I am a beginner, so I can use a lot of help. Thanks!

  2. Anthony Stedman

    Hi Diane,
    Thank you for putting this code up. I am a newbie at this and am having some difficulty in running the code...
    I have followed your instructions to the letter but have made a change to the save path. When I run the code, I dont get a result and in fact, get a run error. The run error is runtime error... '-2147286788 (800300fc)':

    I have altered the save path to
    sPath = enviro & "C:\Users\Anthony\Desktop\Email Saved"

  3. Anthony Stedman

    Hi Diane,

    I have gone with the vanilla approach in the absence of a better understanding of VBA... I will need to spend some more time on this for my sake (And perhaps for yours as well :-)

    I would love to be able to tell Outlook to save this email to a specific location on our server so that we can extend the same code to all 6 machines in the office.

    I saw this code example on one of your pages but have no idea how to insert same into the context of the larger string I was referring to earlier...

    Note: this function will work in any Office application, it is not specific to Outlook. (Actually, it's not specific to Office either, it's a general VB function.)

    Dim strFolderpath as String
    strFolderpath = "C:\OLAttachments\"
    Or use this to save to a folder using the user's profile in the path (example, C:\Users\Diane\)
    Dim enviro As String
    enviro = CStr(Environ("USERPROFILE"))
    strFolderpath = enviro & "\OLAttachments\"

    My file path to the server is L:\Client Filing\Email Filing

    Any assistance on this would be truly appreciated.

    Cheers, Anthony

  4. Anthony Stedman

    Hi Diane

    Thank you for coming back to me. You are an incredibly generous person with your time and I appreciate that.

    We all use the L drive in the office.

    I will fiddle with this code as per your suggestion.

    Cheers again. I don't know how to thank you for your time running this site. It has been invaluable to me,...


  5. James Matthews

    Diane - I keep getting an error message and the debug is showing a problem with the line: oMail.SaveAs sPath & sName, olMSG. When I scroll over "olMSG", I'm seeing olMSG = 3. Also, does it matter if the VBA is in Module1 or in ThisOutlookSession? Thank you.

  6. Javier

    It is possible a variation of the code to loop through all the sub folders that are in the inbox, and save all messages to local disk with the name of each folder, for example:

    \user\inbox\ ---> all messages that are in my inbox
    \user\inbox\ client 1 ---> all messages that are in my folder client 1 (sub folder of Inbox),
    and so on.

    Also save sent items.

    Any idea how I can modify the code to do this.

    Thanks in advance.

    Best regards,

  7. Caroline Marie

    Hi Diane,

    Is there a way that, instead of hardcoding the path, a dialog box appears and prompts the user to choose where to save the email?

    I used to have a macro that would do so (using the SAFRCFileDlg.dll), but it doesn't work under Windows 7.


  8. Caroline Marie

    Hi Diane,

    It works perfectly, thanks! :)

    However, I forgot to mention something important, when I asked for "a dialog box [that] appears and prompts the user to choose where to save the email".

    My previous VBA code (Windows XP) asked the user where to save the file and we could change the filename before saving it (example:

    We have clients who love (a little too much...) writing long subjects, so saving the whole e-mail subject (as of now) may be a problem (especially because our paths on our network are also awfully long). And my coworkers told me that they often changed the filename before saving their emails with the previous macro.

    Huge thanks in advance!

  9. Fred

    Is there a way to have your code run from the rules wizard? I opened VBA from outlook, added a module and pasted your script. When i go to the rules manager i create a new rule and use the "Run a Script" as the action but when the pop up window appears to select the script to run its blank.

    any insight would be appreciated,

  10. Javier

    Hi Diane,

    I tried to send a reply in more than three times, and does not work, any reason...?


  11. Mark

    Diane, I am trying to use this macro to save files to My Documents on my C: Drive. I added a folder titled MailSave and replaced "\Documents\" with "\My Documents\MailSave\", but it didn't seem to work. Am I missing another change in your awesome macro? Thanks!

  12. Harald N

    Hi Diane Poremsky
    I am also beginner at this and hoping very much for your help. I would like to have as Caroline Marie to have "a dialog box [that] appears and prompts the user to choose where to save the email" I have tried to ad the function BrowseForFolder and it prompts okay but does not save the file. I have read the comments but can't make it Work. Can you help is it wrong to implement the function ?
    Hope you will help me, thanks.


  13. Stephen


    Thank you for making this code available, is very good and working well for us.

    Is it possible to add something to the code so that the mail item is permanently deleted rather than moved to the deleted items folder ?


  14. James Matthew

    Diane - any idea why this works fine on Outlook 2010 and doesn't work for me using Outlook 2007? Both are running Windows 7. With OL 2007, I keep getting the error, "Run-time error '-2147287037 (80030003)': The operation failed.

    When I run Debug, it points to this line: oMail.SaveAs sPath & sName, olMSG.

    My sPath is sPath = enviro & "\Desktop\Outlook\".

    The Immediate window shows \Desktop\Outlook\20140318-164025-RE_ Investigative Report.msg. That turns out to be the first line of the selection but it never copies to the \Desktop\Outlook\ folder.

    Thanks so much for your help.

  15. James Matthew

    The folder does exist in that path. I finally got it to work using the complete path C:\Users\username\........". Is there a change I can make in the code to include calendar items when they're selected as well? Thanks again.

  16. Kurt S

    This is GREAT.

    One thing that would really help me is to populate the file attribute/properties. Like put the subject in the Subject Property, etc. That way I can sort easily in folder view, rather than having to pull it back into Outlook sort and search.

    If that cant be done, can you move the "RE: " from the front of the subject to the end, to sort alphabetically and keep threads together?


  17. Kurt S

    If I understand the code correctly, I would use the original code to save the email as a .msg file, then open it and apply the properties to it.

    Thank you so much. This will make managing my emails so much easier.

  18. Thomas

    Hi Diane,

    for the normal email (oMail), it works great. I have some problem with the oReps. In Outlook 2003 this was also working fine, but in Outlook 2010 the macro won't save any mail with TypeName(objItem) "MailItem", as msg.

Leave a Reply

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