Skip to main content

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.

Best Office Productivity Tools

Kutools for Outlook - Over 100 Powerful Features to Supercharge Your Outlook

🤖 AI Mail Assistant: Instant pro emails with AI magic--one-click to genius replies, perfect tone, multilingual mastery. Transform emailing effortlessly! ...

📧 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 ...

📨 Email Management: Easily Recall Emails  /  Block Scam Emails by Subjects and Others  /  Delete Duplicate Emails  /  Advanced Search  /  Consolidate Folders ...

📁 Attachments ProBatch Save  /  Batch Detach  /  Batch Compress  /  Auto Save   /  Auto Detach  /  Auto Compress ...

🌟 Interface Magic: 😊More Pretty and Cool Emojis   /  Boost Your Outlook Productivity with Tabbed Views  /  Minimize Outlook Instead of Closing ...

👍 One-click Wonders: Reply All with Incoming Attachments  /   Anti-Phishing Emails  /  🕘Show Sender's Time Zone ...

👩🏼‍🤝‍👩🏻 Contacts & Calendar: Batch Add Contacts From Selected Emails  /  Split a Contact Group to Individual Groups  /  Remove Birthday Reminders ...

Over 100 Features Await Your Exploration! Click Here to Discover More.

Read More       Free Download      Purchase
 

 

Comments (11)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Everything works now. Thanks very much.
This comment was minimized by the moderator on the site
I'm glad I could help you.
This comment was minimized by the moderator on the site
Hello,
I pasted your code. Outlook 365 restarted. I've tried several times, but it doesn't work. The subject line remains empty. What could be the problem?
This comment was minimized by the moderator on the site
Hi Karol,

Please check if the corresponding Macro settings are enabled in your Outlook.
Go to File > Options. In the Outlook Options window, select Trust Center in the left pane. In the Trust Center window, select Macro Settings in the left pane, and then select the Enable all macros option in the Macro Settings section and check the Apply macro security settings to installed add-ins box in the Add-ins section.
https://www.extendoffice.com/images/stories/comments/comment-picture-zxm/subject-auto.png
This comment was minimized by the moderator on the site
Добрый день. ДА при добавлении 2 файлов в тему писалось через И.
This comment was minimized by the moderator on the site
Hi Александр,

The following VBA code can help to solve your problem. Please give it a try.
Public WithEvents GExplorer As Explorer
'Updated by Extendoffice 20220131
Public WithEvents GInspectors As Inspectors
Public WithEvents GMail As MailItem
Dim GFileName As String

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
  GFileName = ""
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
  GFileName = ""
End Sub

Private Sub GMail_AttachmentAdd(ByVal Att As Attachment)
  If GMail.Subject = "" Then
    GMail.Subject = GFileName
  Else
    GMail.Subject = GMail.Subject & " AND " & GFileName
  End If
  GFileName = ""
End Sub

Private Sub GMail_BeforeAttachmentAdd(ByVal Attachment As Attachment, Cancel As Boolean)
  On Error Resume Next
  GFileName = Attachment.DisplayName
  GFileName = Left$(GFileName, VBA.InStrRev(GFileName, ".") - 1)
  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
    GFileName = ""
    Exit Sub
  End If
End Sub
This comment was minimized by the moderator on the site
Да получилось. Огромное спасибо за помощь. И еще один вопрос, а можно в конце всего этого, при нажатии кнопки "Отправить" выдавал некое сообщение что то типа "Вы действительно хотите отправить данное сообщение"?
This comment was minimized by the moderator on the site
Да, именно. ТЕ при добавлении или перетягивании двух файлов тема письма автоматом писалась через И
This comment was minimized by the moderator on the site
Добрый день. Подскажите пожалуйста можно ли сделать что бы тема формировалась из 2 и более файлов с разделение И? Может в данном коде дописать или вообще другой существует? Заранее спасибо
This comment was minimized by the moderator on the site
Hi Александр,

Do you mean adding multiple attachments while adding those attachment names at the same time to the subject and then linking them with AND?
This comment was minimized by the moderator on the site
Да. Например перетащил два файла и он их имена написал через И. Именно два файла, больше не надо.
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations