• Outlook User
  • Exchange Admin
  • Office 365
  • Outlook Developer
  • Outlook.com
  • Outlook Mac
  • Common Problems
    • Outlook BCM
    • Utilities & Addins
    • Video Tutorials
    • EMO Archives
    • Outlook Updates
    • Outlook Apps
    • Outlook & iCloud Issues
    • Forums

Use VBA to open Outlook messages stored in the file system

Slipstick Systems

› Developer › Use VBA to open Outlook messages stored in the file system

Last reviewed on February 13, 2018   —  29 Comments

December 26, 2013 by Diane Poremsky 29 Comments

An Outlook user posted a question in Outlook Forums:

I need to open Outlook messages stored in a specific folder, and then get the attachments from those Outlook items.

You can do this. You need to use Windows Scripting Host and Outlook's Application.CreateItemFromTemplate to open the messages. Once open, you can save the attachments or do whatever you need to do to the message.

To use this macro, paste the macro in a module, then set a reference to Microsoft Scripting Runtime in the VB Editor's Tools, References dialog box.

Set a reference to the scripting runtime

You'll need to enter the folder path where the MSG files are stored to the GetMSG macro. The folder where you want to save the attachments is stored in strFolderpath in the ListFilesInFolder macro.

Click in GetMSG and press F5 or Run to use the macro.

This code is not Outlook-specific (except for the code between the two Set openMsg lines) and can be used with Word or Excel.


Sub GetMSG()
' True includes subfolders
' False to check only listed folder
   ListFilesInFolder "E:\My Documents\", True
End Sub

Sub ListFilesInFolder(SourceFolderName As String, IncludeSubfolders As Boolean)
    Dim FSO As Scripting.FileSystemObject
    Dim SourceFolder As Scripting.Folder, SubFolder As Scripting.Folder
    Dim FileItem As Scripting.File
    Dim strFile, strFileType, strAttach As String
    Dim openMsg As MailItem

Dim objAttachments As Outlook.Attachments
Dim i As Long
Dim lngCount As Long
Dim strFolderpath As String

'where to save attachments
strFolderpath = "E:\My Documents\attachments\"
    
    Set FSO = New Scripting.FileSystemObject
    Set SourceFolder = FSO.GetFolder(SourceFolderName)
    
    For Each FileItem In SourceFolder.Files
    
    strFile = FileItem.name
      
' This code looks at the last 4 characters in a filename
' If we wanted more than .msg, we'd use Case Select statement
strFileType = LCase$(Right$(strFile, 4))
  If strFileType = ".msg" Then
    Debug.Print FileItem.Path
    
Set openMsg = Application.CreateItemFromTemplate(FileItem.Path)
openMsg.Display
    'do whatever
    
Set objAttachments = openMsg.Attachments
    lngCount = objAttachments.count
         
    If lngCount > 0 Then
     
    For i = lngCount To 1 Step -1
     
    ' Get the file name.
    strAttach = objAttachments.Item(i).FileName
     
    ' Combine with the path to the Temp folder.
    strAttach = strFolderpath & strAttach
     
    ' Save the attachment as a file.
    objAttachments.Item(i).SaveAsFile strAttach
     
    Next i
    End If
  openMsg.Close olDiscard
  
Set objAttachments = Nothing
Set openMsg = Nothing

' end do whatever
      End If
    Next FileItem
    If IncludeSubfolders Then
        For Each SubFolder In SourceFolder.SubFolders
            ListFilesInFolder SubFolder.Path, True
      Next SubFolder
    End If
    
    Set FileItem = Nothing
    Set SourceFolder = Nothing
    Set FSO = Nothing
     
End Sub

How to use the macro

First: You will need macro security set to low during testing.

To check your macro security in Outlook 2010 or 2013, go to File, Options, Trust Center and open Trust Center Settings, and change the Macro Settings. In Outlook 2007 and older, it’s at Tools, Macro Security.

After you test the macro and see that it works, you can either leave macro security set to low or sign the macro.

Open the VBA Editor by pressing Alt+F11 on your keyboard.

To put the code in a module:

  1. Right click on Project1 and choose Insert > Module
  2. Copy and paste the macro into the new module.

More information as well as screenshots are at How to use the VBA Editor

More Information

Accessing Files with FileSystemObject

Use VBA to open Outlook messages stored in the file system was last modified: February 13th, 2018 by Diane Poremsky
  • Click to share on Twitter (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on Google+ (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Share on Skype (Opens in new window)
  • Click to share on Pocket (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to print (Opens in new window)

Related Posts:

  • Import Messages from File System into Outlook Folders
  • Save and Open an Attachment using VBA
  • Save Messages and Attachments to a New Folder
  • Shortcuts to open Outlook folders

About 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.

Leave a Reply

29 Comments on "Use VBA to open Outlook messages stored in the file system"

2500
Photo and Image Files
 
 
 
Audio and Video Files
 
 
 
Other File Types
 
 
 
2500
Photo and Image Files
 
 
 
Audio and Video Files
 
 
 
Other File Types
 
 
 

  Subscribe  
newest oldest most voted
Notify of
Benny
Benny
Share On TwitterShare On Google

Hi Diane, i have a problem when load an message create with a personal module. When it is load I don't find custom property . This is the example:
Dim milk as MailItem
Set mi = Application.CreateItemFromTemplate("c:\miaMail.msg")
Dim par as string
Par = mi.ItemProperties.Item("MIAPROPRIETA").value

But ItemProperties don't have item. Why?
Thank.

Vote Up00Vote Down Reply
September 18, 2017 8:31 am
Premanshu Basak
Premanshu Basak
Share On TwitterShare On Google

Hi Diane,

First of all thank you so much for this piece of code which has helped me a lot in one automation that I am working on. However I am stuck at a place where instead of opening the mail message with ".Display" I want to do a "Reply All". When I try to do that I get an error saying "Run time error '-2147352567 (80020009)': Could not send the message".

Kindly help me with this please. I am using Excel 2013 to run this code.

Regards,
Premanshu

Vote Up00Vote Down Reply
March 15, 2017 9:33 am
Diane Poremsky
Diane Poremsky
Share On TwitterShare On Google

when you open the message using the code, it should be appear as a new message draft, so no need to reply all - it's ready to send.

Vote Up00Vote Down Reply
March 16, 2017 12:17 am
Tash
Tash
Share On TwitterShare On Google

Thank you for the code and it works well, however when the email msg is displayed only the attachments are shown. I wish to extract the attachments and then save the email without attachments to my local drive. Thank you and any assistance is welcome.

Vote Up00Vote Down Reply
January 25, 2017 9:03 pm
Diane Poremsky
Diane Poremsky
Share On TwitterShare On Google

After saving the attachments, you need to delete them. objAttachments.Item(i).Delete

I have a code sample here - https://www.slipstick.com/developer/code-samples/delete-attachments-messages/

Vote Up00Vote Down Reply
January 25, 2017 10:13 pm
Tash
Tash
Share On TwitterShare On Google

Thank you for your quick response. I am not having issues removing the attachments. Once the attachments have been removed I am trying to save the email. The problem seems to be in in "Set openMsg = Application.CreateItemFromTemplate(FileItem.Path)
openMsg.Display" - when this is activated the email opens without any body viewable thus when saving the email it is saved without the body of the email. Again thank you for your time and assistance

Vote Up00Vote Down Reply
January 26, 2017 5:09 pm
Diane Poremsky
Diane Poremsky
Share On TwitterShare On Google

How are you removing the attachments? Can you still see the body after they are removed?

Vote Up00Vote Down Reply
March 16, 2017 12:57 am
Scott
Scott
Share On TwitterShare On Google

This works amazingly!!! Thanks you very much for this. I was wondering if it would be possible to save the attachments as the subject line of the emails they were contained within rather than the names they already have?

Vote Up00Vote Down Reply
June 24, 2016 5:15 am
Diane Poremsky
Diane Poremsky
Share On TwitterShare On Google

you'd change the file name -
' Get the file name.
strAttach = objAttachments.Item(i).FileName
to strAttach = openMsg.Subject

if you have a problem with illegal characters in the subject, there is a function at the end of the macro at https://www.slipstick.com/developer/code-samples/save-messages-and-attachments-in-a-new-folder/ that can be used with this macro to strip the characters.

Vote Up00Vote Down Reply
June 24, 2016 11:37 pm
Jazz
Jazz
Share On TwitterShare On Google

Hello Diane,
I would like an email that i move to draft to automatically open.
If file is already open then it can disregard rule.

Is there a way to do this?

Vote Up00Vote Down Reply
May 25, 2016 2:30 am
Diane Poremsky
Diane Poremsky
Share On TwitterShare On Google

you can use an itemadd macro to watch the drafts folder and display messages that are added to it but I'm not sure about how complicated it would be to skip messages already open.

Vote Up00Vote Down Reply
June 24, 2016 11:39 pm
meir rotfleisch
meir rotfleisch
Share On TwitterShare On Google

Hi Diane,

I have code that saves an email from inside Access VBA to the file system. When I use the Set Msg = objOL.CreateItemFromTemplate(thisfile) I get an error saying the file is open or you don't have permission ..

I have tried this with outlook being open and being closed still not able to access the msg.

Looking forward to your response

Vote Up00Vote Down Reply
April 27, 2016 7:25 am
Eric Bussen
Eric Bussen
Share On TwitterShare On Google

Diane,

They are .msg files not meeting requests. They have .pdf attachments. The .msg files are however custom forms and perhaps this is why the script is failing.

You mention that I might not have Outlook properly referenced. How would this be done. I set the reference you stated above regarding Microsoft scripting runtime. I even checked all the other ones you have checked in the example above thinking that might help. Still a fail. I appreciate the assistance and hope you have a good morning. Thanks,

Vote Up00Vote Down Reply
October 7, 2014 10:24 am
Diane Poremsky
Share On TwitterShare On Google

Are you running the macro from Excel? You need to dim and set Outlook.Application - I'm sure you've done that because you'd get an error about an undefined object otherwise.

it's possible the custom form is the problem. I'll try to look into it in the morning.

Vote Up00Vote Down Reply
October 8, 2014 1:56 am
Eric Bussen
Eric Bussen
Share On TwitterShare On Google

Greetings Diane,

I am trying to use this macro and have followed all your instructions. I get an error message that says Run-time error '438' Object doesn't support this property or method and the debugger highlights this line,

Set openMsg = Application.CreateItemFromTemplate(FileItem.Path)

I am trying to run this using Excel 2010 and the folders I am using for the .msg files and for the attachments are both on my C drive. I have spent considerable time trying to find the answer on my own, but to no avail. Any suggestions for cause and solution would be greatly appreciated. Thanks!!

Vote Up00Vote Down Reply
October 6, 2014 12:10 pm
Diane Poremsky
Share On TwitterShare On Google

Is the message an email message? If it's a meeting request or a report (read receipt, NDR etc) it won't work.

Or, if you are using this with Excel, you don't have Outlook properly referenced. It's not quite the right error for a reference problem though.

Vote Up00Vote Down Reply
October 7, 2014 10:12 am
Marcus
Marcus
Share On TwitterShare On Google

Since you're running from Excel, add "Outlook.":

Set openMsg = Outlook.Application.CreateItemFromTemplate(FileItem.Path)

Vote Up00Vote Down Reply
March 9, 2015 7:32 pm

Visit Slipstick Forums.
What's New at Slipstick.com

Latest EMO: Vol. 23 Issue 10

Subscribe to Exchange Messaging Outlook






Our Sponsors

  • Popular
  • Latest
  • Week Month All
  • Adjusting Outlook's Zoom Setting in Email
  • This operation has been cancelled due to restrictions
  • How to Remove the Primary Account from Outlook
  • Pictures Don't Display in Outlook Messages
  • To Cc or Bcc a Meeting Request
  • Outlook is Not Recognized as the Default Email Client
  • Remove a password from an Outlook *.pst File
  • Understanding Outlook's Auto-Complete Cache (*.NK2)
  • Exchange Account Set-up Missing in Outlook 2016
  • The Signature or Stationery and Fonts button doesn't work
  • Security Certificate Warning in Microsoft Outlook
  • iCloud error: Outlook isn't configured to have a default profile
  • Setting Custom Reminder Times
  • Add Additional Addresses to Room Mailboxes
  • Create a Task from a Message and include the Attachment
  • Forward email messages by date
  • Open multiple Outlook windows when Outlook starts
  • Office 365 Fraud Detection Checks
  • Outlook Request: Calendar Details View
  • Outlook's "Not Junk" option isn't available
Ajax spinner

Newest VBA Samples

Open multiple Outlook windows when Outlook starts

Set most frequently used Appointment Time Zones

How to change the From field on incoming messages

VBA: File messages by client code

Update Contact Area Codes

Set a reminder on selected items in the To-Do List

Replicate GTD: Create a task after sending a message

Use VBA to read fields in attached messages

Move Outlook Folders using VBA

Replicate Smart Lookup using a macro

Recent Bugs List

Microsoft keeps a running list of issues affecting recently released updates at Fixes or workarounds for recent issues in Outlook for Windows.

Windows 10 Issues

  • iCloud, Outlook 2016, and Windows 10
  • Better Outlook Reminders?
  • Coming Soon to Windows 10: Office 365 Search
  • Outlook Links Won’t Open In Windows 10
  • BCM Errors after Upgrading to Windows 10
  • Outlook can’t send mail in Windows 10: error Ox800CCC13
  • Missing Outlook data files after upgrading Windows?

Outlook 2016 Top Issues

  • The Windows Store Outlook App
  • Emails are not shown in the People Pane (Fixed)
  • Calendars aren’t printing in color
  • The Signature or Stationery and Fonts button doesn’t work
  • Outlook’s New Account Setup Wizard
  • BCM Errors after October 2017 Outlook Update
  • Excel Files Won’t Display in Reading Pane
  • Outlook 2016: No BCM
  • Exchange Account Set-up Missing in Outlook 2016

VBA Basics

How to use the VBA Editor

Work with open item or selected item

Working with All Items in a Folder or Selected Items

VBA and non-default Outlook Folders

Backup and save your Outlook VBA macros

Get text using Left, Right, Mid, Len, InStr

Using Arrays in Outlook macros

Use RegEx to extract message text

Paste clipboard contents

Windows Folder Picker

Custom Forms

Designing Microsoft Outlook Forms

Set a custom form as default

Developer Resources

Developer Resources

Developer Tools

Outlook-tips.net Samples

VBOffice.net samples

OutlookCode.com

SlovakTech.com

Outlook MVP David Lee

MSDN Outlook Dev Forum

Repair PST

Convert an OST to PST

Repair damaged PST file

Repair large PST File

Remove password from PST

Merge Two Data Files

Sync & Share Outlook Data

  • Share Calendar & Contacts
  • Synchronize two computers
  • Sync Calendar and Contacts Using Outlook.com
  • Sync Outlook & Android Devices
  • Sync Google Calendar with Outlook
  • Access Folders in Other Users Mailboxes
  • “Live” Group Calendar Tools

Convert to / from Outlook

  • Converting Messages and Calendar or
    Address books
  • Moving Outlook to a New Computer
  • Moving Outlook 2010 to a new Windows computer
  • Moving from Outlook Express to Outlook

Recover Deleted Items

  • Recover deleted messages from .pst files
  • Are Deleted Items gone forever in Outlook?

Outlook 2013 Absolute Beginner's Guide

Diane Poremsky [Outlook MVP]

Make a donation

Calendar Tools

Schedule Management

Calendar Printing Tools

Calendar Reminder Tools

Calendar Dates & Data

Time and Billing Tools

Meeting Productivity Tools

Duplicate Remover Tools

Mail Tools

Sending and Retrieval Tools

Mass Mail Tools

Compose Tools

Duplicate Remover Tools

Mail Tools for Outlook

Online Services

Productivity

Productivity Tools

Automatic Message Processing Tools

Special Function Automatic Processing Tools

Housekeeping and Message Management

Task Tools

Project and Business Management Tools

Choosing the Folder to Save a Sent Message In

Run Rules on messages after reading

Help & Suggestions

Outlook Suggestion Box (UserVoice)

Slipstick Support Services

Contact Tools

Data Entry and Updating

Duplicate Checkers

Phone Number Updates

Contact Management Tools

Sync & Share

Share Calendar & Contacts

Synchronize two machines

Sharing Calendar and Contacts over the Internet

More Tools and Utilities for Sharing Outlook Data

Access Folders in Other Users Mailboxes

View Shared Subfolders in an Exchange Mailbox

"Live" Group Calendar Tools

Home | Outlook User | Exchange Administrator | Office 365 | Outlook.com | Outlook Developer
Outlook for Mac | Outlook BCM | Common Problems | Utilities & Addins | Tutorials
Outlook & iCloud Issues | Outlook Apps
EMO Archives | About Slipstick | Advertise | Slipstick Forums
Submit New or Updated Outlook and Exchange Server Utilities

Send comments using our Feedback page
Copyright © 2018 Slipstick Systems. All rights reserved.
Slipstick Systems is not affiliated with Microsoft Corporation.

You are going to send email to

Move Comment