How to Auto Save Specific Email Attachments with Outlook VBA

Many users hope that Outlook can auto look for specific words in the attachment names and then save the specific attachments to a local folder on their computers. This article will teach you how to accomplish it with Outlook VBA.

In my daily works, if the attachments in received emails are not very important, I prefer not to save them to my local disk, just read them in Outlook directly. But if the attachments are important, such as personnel regulations, monthly working report, I will save them to a specific folder on my computer. In such a case, I wish to configure Outlook to automatically save the email attachments, whose names contain specific words, to a certain folder. So I find out a quick method to realize it with Outlook VBA. Here are the elaborate VBA codes and steps:

Auto Save Specific Email Attachments

  1. Firstly, you should launch Outlook and go to “Developer” tab.
  2. Subsequently, click on the “Visual Basic” button in the “Code” group.Visual Basic Button
  3. Then in the “Visual Basic” window, you should open “ThisOutlookSession” project” and copy the following codes into it.
Public WithEvents olItems As Outlook.Items

Private Sub Application_Startup()
    Set olItems = Session.GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub olItems_ItemAdd(ByVal Item As Object)
    Dim NewMail As Outlook.MailItem
    Dim Atts As Attachments
    Dim Att As Attachment
    Dim strPath As String
    Dim strName As String
 
    If Item.Class = olMail Then
       Set NewMail = Item
    End If
 
    Set Atts = Item.Attachments
 
    If Atts.Count > 0 Then
       For Each Att In Atts
           'Replace "test" with what you want to look for in attachment name
           If InStr(LCase(Att.FileName), "test") > 0 Then
              'Use your wanted destination folder path to save the attachments
              strPath = "C:\Attachments\"
              strName = NewMail.Subject & " " & Chr(45) & " " & Att.FileName
              Att.SaveAsFile strPath & strName
           End If
       Next
    End If
End Sub

Auto Save Specific Email Attachments

  1. After that, you should sign this code.
  • Firstly, use Digital Certificate for VBA Projects to create a certificate.Digital Certificates for VBA Projects
  • Then assign the certificate to this code, shown as the following picture:Sign This Code
  1. After that, you can exit the current “Visual Basic” window and click “Macro Settings” button in “Code” group under “Developer” tab.Enable Digitally Signed Macros Only
  2. From now on, Outlook will automatically save the attachments, whose names have the specific words, to your specified local folder.

Recover from Severe Outlook Corruption

Despite boasting of multiple functions, Outlook is still prone to corruption. Thus you should keep tabs on all issues which can occur to your Outlook, such as PST mail error, Outlook not responding and so on. When confronted with the errors, you can make use of Outlook built-in repair tool, ScanPST.exe to fix them. If it fails, then take recourse to a more powerful tool.

Author Introduction:

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

One response to “How to Auto Save Specific Email Attachments with Outlook VBA”

Leave a Reply

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