Sometimes, when you forward an email that contains several attachments, you may want to rename the attachments directly when forwarding. This article will teach you how to accomplish it with Outlook VBA.
By default, Outlook doesn’t permit user to rename attachments when forwarding. Therefore, if you would like to rename them, the unique way is to first save the files to a local folder on your computer, then rename them and next re-attach them to the forwarding email. Obviously this method is considerably troublesome. Moreover, sometimes, these files may be inessential for you, so you really don’t want to save them to your computer. In this situation, you must long for a quicker method to let you rename the attached files directly in the forwarding emails. The following is an approach to realize it through Outlook VBA.
Rename the Attachments When Forwarding an Email
- In the first place, launch Outlook and head to “Developer” tab.
- Then locate and click on “Visual Basic” button.
- After that, in the new popup window, open a new module by selecting “Insert” > “Module”.
- Subsequently, you ought to copy and paste the following VBA codes into it.
Sub RenameAttachmentsWhenForwarding() Dim olItem As MailItem Dim Att As Attachment Dim Atts As Attachments Dim olForward As MailItem Dim FWAtt As Attachment Dim FWAtts As Attachments Dim FSO As Object Dim TempFPath As Object Dim FilePath As String Dim strName As String Dim strExten As String Dim strFile As String Set olItem = Application.ActiveExplorer.Selection.Item(1) Set Atts = olItem.Attachments Set olForward = olItem.Forward olForward.Display On Error Resume Next For Each Att In Atts 'Get the path to Temporary Folder Set FSO = CreateObject("Scripting.FileSystemObject") Set TempFPath = FSO.GetSpecialFolder(2) FilePath = TempFPath.Path & "\" 'Rename the attachments strName = InputBox("Enter a new name for" & vbCrLf & Att.FileName) 'Change "4" based on the length of the attachment file extension strExten = Right(Att.FileName, 4) strFile = FilePath & strName & "." & strExten If strName <> "" Then 'Save the attachments to the Temporary Folder Att.SaveAsFile (strFile) 'Add the attachments saved in new names from the Temporary Folder olForward.Attachments.Add (strFile) Set FWAtts = olForward.Attachments 'Remove the original attachments For Each FWAtt In FWAtts If InStr(FWAtt.FileName, Att.FileName) > 0 Then FWAtt.Delete End If Next End If Next End Sub
- After that, you can exit the current “Visual Basic” window and proceed to add the new macro to the Quick Access Toolbar as usual.
- Finally you can have a try.
- Firstly, select an email and then click the new macro button in the Quick Access Toolbar.
- Then a new forwarding email will open up. You can see the attached files in original names in the “Attached” line and also receive a dialog box that demands you to enter a new name for a specific attachment.
- After specifying new names for all the files and click “OK”, you will see that all the attached files are in the new names.
- Eventually you can compose the forwarding emails and hit “Send” button to send the message out.
Stay Alert to All Potential Threats to Your Outlook
It is recognized that Outlook is susceptible to corruption. Thus it is a long-drawn task to protect Outlook data against all threats, including virus infection, malware attack, and Outlook PST data damage, etc. The most important action is to make a regular backup for your Outlook data. Also, it is prudent to keep a top-of-the-line repair tool in vicinity, like DataNumen Outlook Repair, which will come in handy.
Author Introduction:
Shirley Zhang is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including repair damaged mdf and outlook repair software products. For more information visit www.datanumen.com
1
hi indeed works greate, only agree with 2nd comment – picture attachments not needed, only real attachments.
Hello,
It worked once and after I reboot my computer now it doesn’t work anymore.
Is there anything I can do to test the VBA code
My Outlook is in French but I don’t thing there is something about that because the firste time I used it it worked well.
Thank you
website
Hi works great, although also offers to rename embedded images (e.g. logos in sign-offs) not just the ‘attachments’.
Appreciate that this is a pretty old post, but I was wondering if there is a way to modify the code to work with any attachments when composing an email, not just forwarding an existing email with attachments. e.g. even in a new email where you have added attachments, but want to rename within the email only, and not have to rename the original file before attaching?
Thanks,
Martin