Edit (June 29,2017): I have made a couple minor changes to the code. One resolves the issue with to-be-renamed attachments being removed from the well when you don’t rename all attachments. The other may resolve the issue with the “path does not exist” error that some have gotten. I can’t be sure because I am unable to reproduce the error.
I often send PowerShell scripts via email. But because .ps1 is a Level 1 attachment, the recipient, if using Outlook, won’t be able to access it (by default). This means I need to rename the file before attaching it to the message. I don’t like doing that, however, because then I have to rename it back after attaching it. I can also compress (zip) the file or make a copy and rename that, but I still have extra files that I then have to delete. I would much rather deal with the file in the context I am sending it: Outlook.
I wrote a VBA macro several years ago that allows me to rename attachments after adding them to the message. Recently, I had an opportunity to share it with somebody so I took the time to update it to accommodate additional scenarios. For example, I discovered that when replying/forwarding a message that has inline images, those are included in the attachment collection, so I had to find a way to exclude those. The same is true when an embedded message is attached. Since the macro was being shared with others, I also added some additional error detection logic to deal with scenarios that normally wouldn’t apply to me or I could deal with one-off.
To use the macro, paste it into ThisOutlookSession in the VBA editor and save it. I modified the ribbon of the new message form (just using the built-in ribbon designer) to add a button that I labeled Rename Attachments and used one of the available icons (there aren’t a lot to choose from), clicking the button runs the macro. (You have to change the default macro security settings in the Trust Center because it isn’t digitally signed.)
Sub RenameAttachmentsWithPrompt() Dim olkItem As Outlook.MailItem Dim olkAttachment As Outlook.Attachment, olkAttachments As Outlook.Attachments Dim objFSO As Object, strFolder As String, strFilename As String Dim olkPA As PropertyAccessor Dim arrRenamedAttachments() As String Dim bValidAttachment, bRenamedAttachment As Boolean If Application.ActiveInspector Is Nothing Then Set olkItem = Application.ActiveExplorer.Selection.Item(1) Else Set olkItem = Application.ActiveInspector.CurrentItem End If i = 1 bValidAttachment = False If olkItem.Attachments.Count > 0 Then If MsgBox("Do you want to rename any of the attachments?", vbYesNo, "Rename any attachments?") = vbYes Then Set olkAttachments = olkItem.Attachments Set objFSO = CreateObject("Scripting.FileSystemObject") 'Bind to the system temp folder strFolder = objFSO.GetSpecialFolder(2) 'Loop through attachments in reverse to account for 'collection size changing when deleting an attachment For j = olkAttachments.Count To 1 Step -1 Set olkAttachment = olkAttachments.Item(j) Set olkPA = olkAttachment.PropertyAccessor Dim bHidden As Boolean 'Visible attachments usually don't have hidden property set so ignore error On Error Resume Next bHidden = olkPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x7FFE000B") On Error GoTo 0 'Skip embedded messages and hidden attachments If olkAttachment.Type <> olEmbeddeditem And (Not bHidden Or IsNull(bHidden)) Then bValidAttachment = True strFilename = "" strFilename = InputBox("What do you want to name the attachment?" & _ vbNewLine & vbNewLine & "(To leave it as is, click OK or Cancel.)", _ "Rename Attachment", olkAttachment.FileName) If strFilename <> olkAttachment.FileName And strFilename <> "" Then 'Attachment file names are read-only, so save renamed attachment to disk 'and delete original from message ReDim Preserve arrRenamedAttachments(i - 1) olkAttachment.SaveAsFile strFolder & "\" & strFilename olkAttachment.Delete arrRenamedAttachments(i - 1) = strFolder & "\" & strFilename bRenamedAttachment = True i = i + 1 End If End If Next If Not bValidAttachment Then noValid = MsgBox("There are no attachments that can be renamed." & vbNewLine & vbNewLine & _ "(Only embedded messages or hidden attachments are in the message.)", vbOKOnly) End If 'Add renamed attachment(s) to message and delete from temp folder If bRenamedAttachment Then For Each strFilePath In arrRenamedAttachments olkItem.Attachments.Add strFilePath objFSO.DeleteFile strFilePath Next End If End If Else noAttach = MsgBox("There are no attachments in the message.", vbOKOnly) End If Set olkItem = Nothing Set olkAttachment = Nothing Set olkPA = Nothing Set objFSO = Nothing End Sub