I want it to only do it when I click the macro.
How do I amend the code to do just that?
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
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: