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 Attachments
- For a start, launch your Outlook program as normal.
- Then press the “Alt + F11” key buttons to access VBA editor.
- In the subsequent “Microsoft Visual Basic for Applications” window, you can find and double click on the “ThisOutlookSession” project on the left side.
- 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 Next 'Move the email to specific folder objMail.Move objTargetFolder End If End If End Sub
- 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.
- After that, you can exit the VBA editor and change Outlook macro security to low.
- Later you ought to restart Outlook to activate the new VBA project.
- Eventually, from now on, Outlook will auto check the attachments’ filenames of all the incoming emails and move the specific ones to specific folders.
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 www.datanumen.com
Leave a Reply