Ella saw my macro that adds attachment names to the reply and asked about a macro that will add the attachment names to the message body before sending:
When I write an email (with attachments) and send it to someone it would be very helpful to have Outlook automatically include a list of the attachments (filenames) at the beginning of my email text body before finally sending it out.This way the recipient cannot accidently miss/overlook the attachment when I do not specifically mention it in the email. When the recipient replies with my original message included I can quickly check what attachments I included with my first email without looking it up in the "sent mail". When including a bunch of attachments (let's say 10 jpg pictures) the recipient overlooks the last attachments and is not aware of them since there is no complete list in the text body.
It's actually fairly easy to convert the Reply macro to work with an ItemSend macro to do this as the message is sent, or you could run the macro manually before sending.
As the message is sent, the ItemSend macro checks for attachmwents and if one or more are found, it calls the AddAttachmentNames macro that adds the filenames to the message body. If you include images in your signature, you can skip smaller attachments by uncommenting the lines that check the file size.
To test the macro, send a message that has attachments.
This macro goes in ThisOutlookSession
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean) If Item.Attachments.Count > 0 Then AddAttachmentNames Item End If End Sub
This macro goes in a Module. Right click on ThisOutlookSAession and choose Insert, Module to add a new module.
You'll need to set a reference to the Word Object Library in the VBA Editor's Tools, Reference dialog.
Public Sub AddAttachmentNames(oItem As MailItem) Dim oAtt As Attachment Dim strAtt As String Dim olInspector As Outlook.Inspector Dim olDocument As Word.Document Dim olSelection As Word.Selection strAtt = "" For Each oAtt In oItem.Attachments ' If oAtt.Size > 5200 Then strAtt = strAtt & " <<" & oAtt.FileName & ">> " ' End If Next oAtt Set olInspector = Application.ActiveInspector() Set olDocument = olInspector.WordEditor Set olSelection = olDocument.Application.Selection olSelection.HomeKey Unit:=wdStory olSelection.InsertBefore "See attachments: " &strAtt & vbCrLf & vbCrLf Set oItem = Nothing End Sub
If you want to run the macro manually on specific messages, replace the sub name with the following two lines.
Public Sub AddAttachmentNames()
Dim oItem As MailItem
Then add these lines before strAtt = ""
Set oItem = Application.ActiveInspector.CurrentItem
If oItem.Attachments.Count = 0 Then
Exit Sub
End If
To make it easier to run the macro, add a macro button to the compose mail form window.
Hi there - I know this is an old discussion, but I get a run time error (91) when replying to a message with attachments. Any way to fix this?
This is happening to me as well - were you able to figure this out? It's specific to this line of the code: Set olDocument = olInspector.WordEditor
Were you able to resolve this? I'm running into the same issue.
To show more gratitude, I'm sharing this. I wanted to have my attachment list show after my signature. So I found code that let me do search and replace in the editor object. When I send an e-mail, the attachment list is created (thank you Diane) and then its "positioned" where I've prepared my special {{attachment list}} tag inside either of my two signatures, through the magic of search-and-replace.
Very nice little helper. Just one additional note, as I realized when I send Outlook invites with attachment an error occurs. Therefore I limited the part in ThisOutlookSession to mail items (olMail) only:
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
If Item.Attachments.Count > 0 And Item.Class = olMail Then
AddAttachmentNames Item
End If
End Sub
I anyhow do not need to write attachment names in outlook invites. So this solution works fine with me.
Michael
Hi Diane!
This works great! Could you please help me modify to: 1) list below message body just above signature...below signature would be fine too, and 2) format it (e.g. bold and italicized)?
I do not know VBA at all, but I tried changing InsertBefore to Insert After...that did not work. I've tried to look at other formatting macros to piece together the formatting, but I'm coming up #fail.
Thanks a lot!
putting it at the very end is easy - putting it before or immediately after the signature is more difficult as you need to find the signature. Formatting is easy - it uses standard html (use ' instead of " in the styles) and may need to use HTMLBody. You could also select it and inserting and apply formatting using word code.
olSelection.InsertBefore "See attachments: " &strAtt & "" & vbCrLf & vbCrLf
to move it to the end, you use word code - olSelection.EndKey Unit:=wdStory, Extend:=wdExtend
Also, the script works great! But I'm getting a run-time error "13 - type mismatch" when I try to forward meeting invites w an attached invite.
You only get it on meetings? This line: Public Sub AddAttachmentNames(oItem As MailItem) tells it the item is an mail item. Although you mail meeting requests, they are not mailitems. You could try using oitem as object or an if statement to only use mail items. Put this at the top of the application_itemsend macro.
If Item.MessageClass <> "IPM.Post" Then
Exit Sub
End If
This is great. I know nothing about VBA/macros. Your instructions were clear. I did have to enable macros to run and deactivate DEP. It works perfectly. However, how can I modify the code so that the "see attachments" text is at the bottom of the message?
Try changing insertbefore to insertafter. (I actually forget if that will work, but if not, there are other ways to do it.)
olSelection.HomeKey Unit:=wdStory
olSelection.InsertBefore "See attachments: " &strAtt & vbCrLf & vbCrLf
Hi Diane Firstly, let me say how much I love your site. It’s taught me a lot about MS Outlook macros and VBA. I have used your code "Print Attachments on Selected Messages" to create a macro to print both an email and its attachments and it’s working great! I was wondering, however, how I might print a list of the attachments so that I can check that all the attachments were printed. Ideally I would like to modify the macro to (1) print the email, (2) print a list of the attachments, and then (3) print each attachment. If you have any ideas, I would be so grateful. Regards Martin Private Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias _ "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _ ByVal lpFile As String, ByVal lpParameters As String, _ ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long Sub PrintEmailAttachments() ' Confirmation message Dim OutPut As Integer OutPut = MsgBox("Do you want to print the email attachments?", vbYesNo + vbQuestion + vbDefaultButton2, "Print Email Attachments") If OutPut = 7 Then Exit Sub End If ' Declare variables Dim objMail As Outlook.MailItem Dim obj As Object Dim objAttachment As Outlook.Attachment… Read more »
to get a list of names you'll add
For Each objAttachment In objAttachments
strAtt = strAtt & objAttachment .FileName
Next objAttachment
then you just need to insert it in a file and print.
The sample at https://www.slipstick.com/developer/macro-send-files-email/ shows how to include a file name in a message - although it would probably be better to save it in a text file and print. The second macro at https://www.slipstick.com/developer/code-samples/save-email-message-text-file/ shows how to write data to a text file.
Thankyou Diane that's brilliant :-)
Dear Diane Poremsky,
I learned a lot from your post and deeply appreciated for that.
Please advise me how to save attachment(s)that attached in email into my local folder such as “C:\AttachmentSent\” before sending email automatically.
I just want to keep copies of attachment files that I sent out as back-up copies.
Best regards,
Nguyen
you would use code like this - this example uses an Attachments folder under the user's documents folder, but you can change the path used in strFolderPath as needed.
strAtt = strAtt & " <<" & oAtt.FileName & ">> "
Dim strSavePath As String
strSavePath = CreateObject("WScript.Shell").SpecialFolders(16) & "\Attachments\"
oItem.Attachments.Item(1).SaveAsFile strSavePath & oAtt.FileName
The version loaded is as offered from my 365 Business Account. Path to Microsoft Word 16.0 object library is c:\Program Files\ Microsoft Office\Root\Office16\MSWORD.OL
That's the 64bit version? I wonder if that is the problem - most macros work fine with 64bit but a few need tweaked for 64bit.