Save and Rename Outlook Email Attachments

Last reviewed on May 7, 2014   —  5 comments

A visitor to our forums had a script to save attachments to his hard drive and wanted to add the attachment's modified date to the filename.
Save attachments with the modified date

To get the modified date (or any other file property), you need to use the FileSystem Object, or FSO, to read the properties.

Instead of using the file's modified date, or if Outlook is not getting the expected modified date, you can use the email message's sent date. In many cases, the DateLastModified on the attachments will be the Sent date anyway. A macro using the SentOn field is here.

The first macro saves the attachments on selected messages and changes the names of the saved files to include the modified date. The second macro is used as the script in a rule a script rule.

The macros save the attachments to the user's Documents folder.

Use with selected messages

Public Sub saveAttachtoDisk()
Dim itm As Outlook.MailItem
Dim currentExplorer As Explorer
Dim Selection As Selection

Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim fso As Object
Dim oldName

Dim file As String
Dim DateFormat As String
Dim newName As String

Dim enviro As String
enviro = CStr(Environ("USERPROFILE"))
saveFolder = enviro & "\Documents\Attachments\"

Set currentExplorer = Application.ActiveExplorer
Set Selection = currentExplorer.Selection

Set fso = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
For Each itm In Selection

 For Each objAtt In itm.Attachments
   
 file = saveFolder & objAtt.DisplayName
 objAtt.SaveAsFile file
 
'Get the file name 
 Set oldName = fso.GetFile(file)
 
 DateFormat = Format(oldName.DateLastModified, "yyyy-mm-dd ")
 newName = DateFormat & objAtt.DisplayName
 
 oldName.Name = newName

 Set objAtt = Nothing
 Next
 
 Next
 
 Set fso = Nothing
 End Sub

Use in a Run a Script Rule

Public Sub saveAttachtoDiskRule(itm As Outlook.MailItem)

Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim fso As Object
Dim oldName

Dim file As String
Dim DateFormat As String
Dim newName As String

Dim enviro As String
enviro = CStr(Environ("USERPROFILE"))
saveFolder = enviro & "\Documents\test\"

Set fso = CreateObject("Scripting.FileSystemObject")
On Error Resume Next

 For Each objAtt In itm.Attachments
 file = saveFolder & objAtt.DisplayName
 objAtt.SaveAsFile file
 
 Set oldName = fso.GetFile(file)
 DateFormat = Format(oldName.DateLastModified, "yyyy-mm-dd ")
 newName = DateFormat & objAtt.DisplayName
 oldName.Name = newName

 Set objAtt = Nothing
 Next
  
 Set fso = Nothing
 End Sub

How to use macros

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

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.

5 responses to “Save and Rename Outlook Email Attachments”

  1. Venu

    Macro works fine, but I wanted to save the attachment with subject and sendername also along with date stamp. Subject line can be limited upto first 15 characters if its too long.

  2. Perry Garrod

    Hi
    I'd like to have my saved attachements, which I split out from the email, to have the same date - or within seconds - of the saved mail.
    The attachments I am saving are scanned and them emailed, so it would be nice to be able to see then next to each other in the directory, but the scanned attachments have the scan date & time.
    Is it posssible to save an attachment with either modified or create dates being the date the attachment is actually saved, i.e. same time as the email itself?
    Doing this in VBSO VB
    Thanks
    Perry

  3. Warren Cramton

    I've tried making the suggested change so that it renames the attachment to the email Subject line but I'm not having any luck getting it to work.

    I am an absolute novice at this. Any chance you can edit the first Macro to the exact content I need? The PDF attachment needs to be saved to C:\Temp and renamed to the Subject line of the message.

    This is for Outlook 2013. Thanks in advance.

Leave a Reply

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