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