How to automatically fill email subject with attachment name in Outlook?
For those who are accustomed to using the attachment name as the subject of an email in Outlook, manually typing the attachment name in the subject line each time will be time-consuming. As shown in the gif below, if you want to fill the email subject line with the attachment name automatically when you insert an attachment, the VBA method in this tutorial will help.
Automatically fill the email subject with the attachment name with VBA code
Please run the following VBA code to automatically fill the email subject with the name of the inserted attachment in Outlook.
1. Launch your Outlook, press the Alt + F11 keys to open the Microsoft Visual Basic for Applications window.
2. In the Microsoft Visual Basic for Applications window, double click Project1 (VbProject.OTM) > Microsoft Outlook Objects > ThisOutlookSession to open the ThisOutlookSession (Code) editor.
3. Then copy the following VBA code into the ThisOutlookSession (Code) editor.
VBA code: Autofill the email subject with the attachment name
Public WithEvents GExplorer As Explorer 'Updated by Extendoffice 20220926 Public WithEvents GInspectors As Inspectors Public WithEvents GMail As MailItem Private Sub Application_Startup() Set GExplorer = Application.ActiveExplorer Set GInspectors = Application.Inspectors End Sub Private Sub GExplorer_InlineResponse(ByVal Item As Object) Set GMail = Item End Sub Private Sub GInspectors_NewInspector(ByVal Inspector As Inspector) Dim xItem As Object Set xItem = Inspector.CurrentItem If xItem.Class <> olMail Then Exit Sub Set GMail = xItem End Sub Private Sub GMail_AttachmentAdd(ByVal Att As Attachment) Dim xFileName As String On Error Resume Next If VBA.Trim(GMail.Subject) <> "" Then Exit Sub If MsgBox("Do you want to use the attachment name as the subject?", vbYesNo + vbInformation, "Kutools for Outlook") = vbNo Then Exit Sub xFileName = Att.DisplayName xFileName = Left$(xFileName, VBA.InStrRev(xFileName, ".") - 1) GMail.Subject = xFileName End Sub
4. Now you need to save the code and restart Outlook.
5. From now on, when you insert an attachment in an email that doesn't have a subject filled in yet, a Kutools for Outlook dialog box will pop up asking you if you want to use the attachment name as the subject, click the Yes button.
Then the subject line will be populated with the name of the inserted attachment automatically.
Best Office Productivity Tools
Kutools for Outlook - Over 100 Powerful Features to Supercharge Your Outlook
📧 Email Automation: Out of Office (Available for POP and IMAP) / Schedule Send Emails / Auto CC/BCC by Rules When Sending Email / Auto Forward (Advanced Rules) / Auto Add Greeting / Automatically Split Multi-Recipient Emails into Individual Messages ...
Over 100 Features Await Your Exploration! Click Here to Discover More.