Note: The other languages of the website are Google-translated. Back to English
English English

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.

Notes:

1) If you have filled in the subject already, inserting an attachment won’t pop up the dialog box.
2) If you remove an existing subject from the current email but the cursor is still displayed in the subject line, inserting an attachment will not bring up the dialog box either. After removing the subject, you need to click away from the subject line to refresh the message box to make the code work.
3) If you insert multiple attachments at the same time, only the name of the first attachment can be used as the subject of the email.

Kutools for Outlook - Brings 100 Advanced Features to Outlook, and Make Work Much Easier!

  • Auto CC/BCC by rules when sending email; Auto Forward Multiple Emails by custom; Auto Reply without exchange server, and more automatic features...
  • BCC Warning - show message when you try to reply all if your mail address is in the BCC list; Remind When Missing Attachments, and more remind features...
  • Reply (All) With All Attachments in the mail conversation; Reply Many Emails in seconds; Auto Add Greeting when reply; Add Date into subject...
  • Attachment Tools: Manage All Attachments in All Mails, Auto Detach, Compress All, Rename All, Save All... Quick Report, Count Selected Mails...
  • Powerful Junk Emails by custom; Remove Duplicate Mails and Contacts... Enable you to do smarter, faster and better in Outlook.
shot kutools outlook kutools tab 1180x121
shot kutools outlook kutools plus tab 1180x121
 
Comments (0)
No ratings yet. Be the first to rate!
There are no comments posted here yet
Leave your comments
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations