How to Display Attachment Count in Email List with Outlook VBA

If you would like to quickly get the count of attachments in each email in email list, you can use the VBA code in this article. It’ll auto count the attachments in every incoming email and display the count in a new column.

Some users frequently need to count the email attachments. In this scenario, why not create and enable an exclusive column for displaying the attachment count? In the followings, we will teach you how to make Outlook auto count the email attachments and show the count in such a new column.

Display Attachment Count in Email List with Outlook VBA

Display Attachment Count in Email List

  1. For a start, launch your Outlook application.
  2. Then press “Alt + F11” key buttons in the main Outlook window.
  3. Next in the new Outlook VBA editor window, you need to open a module that is not being used.
  4. Subsequently, copy and paste the following VBA code into this module.
Sub AutoCountAttachments(objMail As Outlook.MailItem)
    Dim objAttachments As Outlook.Attachments
    Dim objProperties As Outlook.UserProperties
    Dim objProperty As Outlook.UserProperty
    Dim strPropertyName As String
 
    Set objAttachments = objMail.Attachments
 
    strPropertyName = "Attachment Count"
    Set objProperties = objMail.UserProperties
 
    'Check if there is already a column in this name
    Set objProperty = objProperties.Find(strPropertyName, True)
    If objProperty Is Nothing Then
       'If not, add this column
       Set objProperty = objProperties.Add(strPropertyName, olText, True)
    End If
 
    'Set the column value equal to the count of attachments
    objProperty.value = objAttachments.Count
    objMail.Save
End Sub

VBA Code - Display Attachment Count in Email List

  1. After that, you can exit the current “Microsoft Visual Basic for Applications” window.
  2. Later you need to create a new rule for running this script on every incoming email.
  • Firstly, go to “File” menu and click on the “Manage Rules & Alerts” button under “Info” tab.
  • Then in the new dialog box, hit the “New Rule” button.
  • In the subsequent “Rule Wizard” screen, select “Apply rule on messages I receive” and hit “Next”.
  • Next ignore the “Select conditions” and click “Next”.
  • Then select “run a script” action and select the previously added macro, like the following screenshot.Select “run a script” action
  • After that, click several “Next” until reaching the final “Finish rule setup” screen.
  • Later, you need to specify a new name for this rule, and enable “Run this rule on messages already in “Inbox”” option and ‘Turn on this rule”.
  • Lastly, click on “Finish” button.Finish Rule Setup
  1. After that, you ought to add the column “Attachment Count” created by the VBA code. Just take the following steps below:
  • First, go to “View” tab in the email list screen.
  • Then click on “View Settings” button.
  • In the newly popup dialog box, hit “Columns” button.
  • Then in the “Show Columns” window, find and select “Attachment Count” column under “User-defined columns in Inbox” and hit “Add” button.Add "Attachment Count" Column
  • Finally click “OK” button to save the changes.
  1. Eventually, you can see a “Attachment Count” column in the email list, which is displaying attachment count for each email, shown as the image below:Attachment Count in Email List

Avoid Frustrating PST Data Loss

It is believed that no one is willing to accept PST data loss. The same must hold true for you. Therefore, you have to make some precautions, such as making data backups of your PST file and getting hold of a reputable fix tool like DataNumen Outlook Repair. It can repair Outlook issues without breaking a sweat.

Author Introduction:

Shirley Zhang is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including sql fix and outlook repair software products. For more information visit www.datanumen.com