This macro allows you to rename attachments on incoming or outgoing messages.
To rename attachments on incoming messages, open the message in a new window then run the macro.
To use with outgoing messages, open the message in a new window and run the macro.
As written, there are three options for renaming:
- The user enters a new name
- The current date is added as a suffix
- The senders name is added as a prefix
When automating the filename change, you can use any value available to the macro, including the sender's name, received date, current date and time, or you can hard code a word to use.
'do not type the extension strNewName = InputBox("Current Value: " & strCurrent, "Rename to", strCurrent) 'if you click cancel, stop the macro If StrPtr(strNewName) = 0 Then Exit Sub ' suffix 'strNewName = strCurrent & Format(Date, " mm-dd-yyyy") 'Debug.Print strNewName ' prefix 'strNewName = Item.SenderName & "-" & strCurrent
Rename Attachment Macro
Public Sub RenameAttach() Dim objApp As Outlook.Application Dim Item As Outlook.MailItem Dim objAtt As Outlook.Attachment Dim strExt As String Dim saveFolder As String Dim enviro As String Dim strOldName As String Dim strNewName As String Dim iCount As Long enviro = CStr(Environ("Temp")) saveFolder = enviro & "\" Debug.Print saveFolder Set objApp = Outlook.Application Set Item = objApp.ActiveInspector.CurrentItem iCount = Item.Attachments.Count For i = iCount To 1 Step -1 Set objAtt = Item.Attachments.Item(i) ' get the last 5 characters for the file extension strOldName = objAtt.DisplayName strExt = Right(strOldName, Len(strOldName) - InStrRev(strOldName, ".") + 1) 'get the file name w/o extension strCurrent = Left(strOldName, InStrRev(strOldName, ".") - 1) Debug.Print strCurrent, strExt 'do not type the extension strNewName = InputBox("Current Value: " & strCurrent, "Rename to", strCurrent) 'if you click cancel, stop the macro If StrPtr(strNewName) = 0 Then Exit Sub ' suffix 'strNewName = strCurrent & Format(Date, " mm-dd-yyyy") 'Debug.Print strNewName ' prefix 'strNewName = Item.SenderName & "-" & strCurrent ' put the name and extension together file = saveFolder & strNewName & strExt objAtt.SaveAsFile file objAtt.Delete Item.Attachments.Add file Next Set objAtt = Nothing End Sub
How to use the macros on this page
First: You need to have macro security set to the lowest setting, Enable all macros during testing. The macros will not work with the top two options that disable all macros or unsigned macros. You could choose the option Notification for all macros, then accept it each time you restart Outlook, however, because it's somewhat hard to sneak macros into Outlook (unlike in Word and Excel), allowing all macros is safe, especially during the testing phase. You can sign the macro when it is finished and change the macro security to notify.
To check your macro security in Outlook 2010 and newer, go to File, Options, Trust Center and open Trust Center Settings, and change the Macro Settings. In Outlook 2007 and older, look 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.
Macros that run when Outlook starts or automatically need to be in ThisOutlookSession, all other macros should be put in a module, but most will also work if placed in ThisOutlookSession. (It's generally recommended to keep only the automatic macros in ThisOutlookSession and use modules for all other macros.) The instructions are below.
The macros on this page should be placed in a module.
Open the VBA Editor by pressing Alt+F11 on your keyboard.
To put the code in a module:
- Right click on Project1 and choose Insert > Module
- Copy and paste the macro into the new module.
More information as well as screenshots are at How to use the VBA Editor
Hello Diana,
I have also similar issue as reported by Kacper, I need to rename certain file that is attached to email, but in addition to that I get jpg files to rename (from signature I believe). Is there a way to bypass it?
If you only use it to change the names of specific attachment types - like only excel or word docs or never jpg and png, an if statement can check the file types and skip them. I'll try to update it later today.
Seems it will just save the attachment as a new name you want, and then re-attach it back into the email. This work in most case except with MSG files.
Yes, that is what it does. It's been so long since I wrote it, I don't recall if I tested with msg files. Will check it.
It saves the msg attachment off - and adds it back, but Outlook uses the subject as the display name, not the file name. To change that, you would need to open the message, change the subject, save as a file then attach.
Hi!
I would like to ask a question regarding this macro.
On the attached screenshots you can see that in the first e-mail there is a footer with an image, and the macro treats it like an attachment is there any way to make some changes in this code to make it avoid those images and would only choose from the attached files in the bar?