Tip: Other languages are Google-Translated. You can visit the English version of this link.
Log in
x
or
x
x
Register
x

or

How to list all attachment names into message body when composing email in Outlook?

Are there any good ways for us to insert all attachment names into message body when composing an email in Outlook? This article, I will talk about how to solve this job in Outlook.

List all attachment names into message body when composing email with VBA code


List all attachment names into message body when composing email with VBA code

Please do with the following steps to finish this task:

1. Hold down the ALT + F11 keys to open the Microsoft Visual Basic for Applications window.

2. In the Microsoft Visual Basic for Applications window, double click ThisOutlookSession from the Project1(VbaProject.OTM) pane to open the mode, and then copy and paste the following code into the blank module.

VBA code: List all attachment names into message body:

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    Dim xMailItem As MailItem
    If Item.Class = olMail Then
        Set xMailItem = Item
        If xMailItem.Attachments.Count > 0 Then
          AddAttachmentNamesToBody
        End If
    End If
    End Sub

3. Then go on clicking Insert > Module, copy and paste below code into the opened blank module, see screenshot:

VBA code: List all attachment names into message body:

Public Sub AddAttachmentNamesToBody()
    Dim xMailItem As MailItem
    Dim xAttachment As Attachment
    Dim xFileName As String
   Dim xInspector As Outlook.Inspector
    Dim xDoc As Word.Document
    Dim xWdSelection As Word.Selection
    On Error Resume Next
    Set xMailItem = Outlook.ActiveInspector.CurrentItem
    If xMailItem.Attachments.Count = 0 Then
        Exit Sub
    End If
    xFileName = ""
    For Each xAttachment In xMailItem.Attachments
        If xFileName = "" Then
            xFileName = " <" & xAttachment.FileName & "> "
        Else
            xFileName = xFileName & vbCrLf & " <" & xAttachment.FileName & "> "
        End If
    Next xAttachment
    Set xInspector = Outlook.Application.ActiveInspector()
    Set xDoc = xInspector.WordEditor
    Set xWdSelection = xDoc.Application.Selection
    xWdSelection.HomeKey Unit:=wdStory
    xWdSelection.InsertBefore "Attachments: " & vbCrLf & xFileName & vbCrLf & vbCrLf
    Set xMailItem = Nothing
    End Sub

4. And then click Tools > References in the Microsoft Visual Basic for Applications window, in the popped out References-Project1 dialog box, check Microsoft Word Object Library option from the Available References list box, see screenshot:

5. Click OK to exit the dialog box, then you should add the macro button into the Quick Access Toolbar. In the new Message window, choose More Commands from the Customize Quick Access Toolbar drop down, see screenshot:

6. In the Outlook Options dialog box, do the following operations:

(1.) Select Macros from the Choose commands from drop down list;

(2.) Click the macro name that you have inserted just now;

(3.) And then click Add button to add the macro into the Customize Quick Access Toolbar.

7. Then click OK to close the dialog box, now, the macro button has been inserted into the Quick Access Toolbar, see screenshot:

8. Now, when you create new message, and click the macro button, the attachment names will be inserted above the message body as following screenshot shown:

 

Recommended Productivity Tools

shot kutools outlook kutools tab 1180x121
shot kutools outlook kutools plus tab 1180x121

Kutools for Outlook - More than 100 Advanced Functions for Outlook, Improve 70% Efficiency For you

  • Complicated and repeated operations can be done a one-time processing in seconds.
  • Forward multiple emails individually with one-click, and auto forward by rules.
  • Auto CC/BCC every sending email and easy for customizing rules, and auto reply without requiring exchange server.
  • Powerful junk emails filter, remove duplicate emails, reply with attachment, bunch of one-click operations, and so on...
  • 60-day unlimited free trial. 60-day money back guarantee. 2 years free upgrade and support. Buy once, use forever.
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    michael williams · 2 months ago
    when I tried this code it sends the attachment names in every email that has attachments.
    I want it to only do it when I click the macro.

    How do I amend the code to do just that?
  • To post as a guest, your comment is unpublished.
    SB · 3 months ago
    This is wonderful -- thank you! Is there also a way to somehow view all the attachment names in an email that has been sent to you from someone else (i.e. received)? For some reason, the file names are not displaying in full unless you hover, which is ridiculous when you regularly have 15 files to sort through.
    • To post as a guest, your comment is unpublished.
      skyyang · 3 months ago
      Hello,
      To List all attachment names in an received email, please copy and pase the below VBA code into the ThisOutlookSession module of the Microsoft Visual Basic for Applications window:

      Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)
      Dim xEIDArr As Variant, xEID As Variant, xItem As Object
      Dim xAttachment As Attachment
      Dim xFileName As String
      On Error Resume Next
      xEIDArr = Split(EntryIDCollection, ",")
      For Each xEID In xEIDArr
      Set xItem = Session.GetItemFromID(xEID)
      If xItem.Class = olMail Then
      xFileName = ""
      For Each xAttachment In xItem.Attachments
      If IsEmbeddedAttachment(xAttachment) = False Then
      If xFileName = "" Then
      xFileName = " " & "<" & xAttachment.FileName & ">"
      Else
      xFileName = xFileName & "
      " & " " & "<" & xAttachment.FileName & ">"
      End If
      End If
      Next xAttachment
      If xFileName = "" Then Exit Sub
      xFileName = "Attachments: " & "
      " & xFileName & "
      " & "
      "
      xItem.HTMLBody = "" & xFileName & "" & xItem.HTMLBody
      xItem.Save
      End If
      Next
      Set xItem = Nothing
      End Sub

      Function IsEmbeddedAttachment(Attach As Attachment)
      Dim xAttParent As Object
      Dim xCID As String, xID As String
      Dim xHTML As String
      On Error Resume Next
      Set xAttParent = Attach.Parent
      xCID = ""
      xCID = Attach.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F")
      If xCID <> "" Then
      xHTML = xAttParent.HTMLBody
      xID = "cid:" & xCID
      If InStr(xHTML, xID) > 0 Then
      IsEmbeddedAttachment = True
      Else
      IsEmbeddedAttachment = False
      End If
      End If
      End Function

      After pasting this code, when new emails with attachments arriving in your Outlook, the attachment names will be listed at the top of the message body automatically.
      Please try it, hope it can help you!