How to Auto Move Incoming Emails to Specific Folders Based on Attachment File Names

Many users hope that Outlook can automatically move and archive the incoming emails to specific mail folders according to the attachment file names. Thus, in this article, we will expose a quick way to help you get it with Outlook VBA.

If you desire to auto move the incoming emails to the specific folders as per their attachments’ filenames, you will definitely think of using Outlook rule in the first place. However, you will finally discover that Outlook rule doesn’t have a feature to check the attachment filename. Therefore, if you indeed would like to realize it, you have to use other ways, for instance, via a third party add-in or Outlook VBA. So here we will teach you how to achieve it with VBA. Read on to get the detailed operations and concrete VBA codes.

Auto Move Incoming Emails to Specific Folders Based on Attachment File Names

Auto Move Incoming Emails to Specific Folders Based on Attachments

  1. For a start, launch your Outlook program as normal.
  2. Then press the “Alt + F11” key buttons to access VBA editor.
  3. In the subsequent “Microsoft Visual Basic for Applications” window, you can find and double click on the “ThisOutlookSession” project on the left side.
  4. Next in the opened “ThisOutlookSession” project window, copy and paste the following VBA codes.
Public WithEvents objMails As Outlook.Items

Private Sub Application_Startup()
 Set objMails = Outlook.Application.Session.GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub objMails_ItemAdd(ByVal Item As Object)
    Dim objMail As Outlook.MailItem
    Dim objAttachments As Outlook.attachments
    Dim objAttachment As Outlook.Attachment
    Dim strAttachmentName As String
    Dim objInboxFolder As Outlook.Folder
    Dim objTargetFolder As Outlook.Folder
    'Ensure the incoming item is an email
    If TypeOf Item Is MailItem Then
       Set objMail = Item
       Set objAttachments = objMail.attachments
       'Check if the incoming email contains one or more attachments
       If objAttachments.Count > 0 Then
          For Each objAttachment In objAttachments
              strAttachmentName = objAttachment.DisplayName
              Set objInboxFolder = Application.Session.GetDefaultFolder(olFolderInbox)
              'Check the names of all the attachments
              'Specify the target folders
              If InStr(LCase(strAttachmentName), "worklog") > 0 Then
                 Set objTargetFolder = objInboxFolder.Folders("WorkLog")
              ElseIf InStr(LCase(strAttachmentName), "report") > 0 Then
                 Set objTargetFolder = objInboxFolder.Folders("Report")
              ElseIf InStr(LCase(strAttachmentName), "statistics") > 0 Then
                 Set objTargetFolder = objInboxFolder.Folders("Statistics")
              End If
         'Move the email to specific folder
          objMail.Move objTargetFolder
       End If
    End If
End Sub

VBA Codes - Auto Move Incoming Emails to Specific Folders Based on Attachment File Names

  1. Subsequently, you need to digitally sign the new VBA project.
  • Firstly, you can use inbuilt tool – Digital Certificates for VBA Projects to create a personal certificate.
  • Then in VBA editor, click “Tools” > “Digital Signature” in the toolbar.
  • Next follow the onscreen instructions to sign it.
  1. After that, you can exit the VBA editor and change Outlook macro security to low.
  2. Later you ought to restart Outlook to activate the new VBA project.
  3. Eventually, from now on, Outlook will auto check the attachments’ filenames of all the incoming emails and move the specific ones to specific folders.Archive Emails Based on Attachment File Names

Safeguard Your PST Data

It is almost an unquestioned fact that Outlook is susceptible to corruption. Hence, it is a quite arduous task to protect your PST data against damage. But you still can find some rules and tips. For instance, to avert PST data loss, you can persist in making a regular data backup. Moreover, in order to provide immediate rescue after Outlook crash, you can prepare an experienced PST fix tool, like DataNumen Outlook Repair.

Author Introduction:

Shirley Zhang is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including SQL Server recovery and outlook repair software products. For more information visit

2 responses to “How to Auto Move Incoming Emails to Specific Folders Based on Attachment File Names”

  1. I relly get stuck on this at row:
    If InStr(LCase(strAttachmentName), “worklog”) > 0 Then
    Doeas this mean that if the attachmentname in the variable “strAttachmentName” = “worklog” then the code will move it?
    I also have issues with:
    objMail.Move objTargetFolder
    I get:
    objTargetFolder = nothing

    when i debug.

  2. Hi, There are 4 members in my team working on Latest Outlook (windows 10)

    I want to assign all incoming emails from my shared mailbox (which is not my default mailbox)
    one by one to 4 folders,
    Folder1, Folder2, Folder3, and Folder4

    i do 8 hours of shift where i need to check incoming email twice a day to assign within the team, please give me a Macro that will automatically allocate all unread emails to the mentioned 4 subfolders under my shared mailbox equally.

Leave a Reply

Your email address will not be published. Required fields are marked *