Save email attachments automatically Using VBA in Outlook

Sorting through each email to find and save attachments can be time-consuming and tedious.

This is where automation comes in handy. With a simple Visual Basic for Applications (VBA) script, Outlook users can automate the process of saving email attachments.

This blog post will guide you through creating and using a VBA script in Outlook to automatically save your email attachments, streamlining your workflow and saving you time.

VBA Code for Saving Email Attachments

Sub SaveAttachmentsToFolder()
    Dim ns As Outlook.NameSpace
    Dim Inbox As Outlook.MAPIFolder
    Dim Item As Object
    Dim Atmt As Outlook.Attachment
    Dim FileName As String
    Dim i As Integer
    Dim SaveFolder As String

    SaveFolder = "C:\EmailAttachments\"
    Set ns = Application.GetNamespace("MAPI")
    Set Inbox = ns.GetDefaultFolder(olFolderInbox)

    For Each Item In Inbox.Items
        For Each Atmt In Item.Attachments
            FileName = SaveFolder & Atmt.FileName
            Atmt.SaveAsFile FileName
            i = i + 1
        Next Atmt
    Next Item

    MsgBox i & " Attachments saved to " & SaveFolder
End Sub

Explaining the VBA Code

Let’s break down this VBA (Visual Basic for Applications) code step by step. This script is designed to save email attachments to a specified folder using Microsoft Outlook.

  1. Subroutine Declaration:
    • Sub SaveAttachmentsToFolder(): This line starts the subroutine (a block of code that performs a specific task) named SaveAttachmentsToFolder.
  2. Variable Declaration:
    • Dim ns As Outlook.NameSpace: Declares ns as a variable to represent the Outlook Namespace, which provides access to all the Outlook folders.
    • Dim Inbox As Outlook.MAPIFolder: Declares Inbox to represent the Inbox folder in Outlook.
    • Dim Item As Object: Declares Item as a generic object; this will be used to represent each email item in the Inbox.
    • Dim Atmt As Outlook.Attachment: Declares Atmt to represent each attachment in an email.
    • Dim FileName As String: Declares FileName as a string variable to store the file path for saving attachments.
    • Dim i As Integer: Declares i as an integer to count the number of attachments saved.
    • Dim SaveFolder As String: Declares SaveFolder as a string to store the path of the folder where attachments will be saved.
  3. Setting Folder Paths:
    • SaveFolder = "C:\EmailAttachments\": Sets SaveFolder to a specific path (here, “C:\EmailAttachments”).
    • Set ns = Application.GetNamespace("MAPI"): Initializes ns with the MAPI namespace, which is used to access Outlook data.
    • `Set Inbox =

ns.GetDefaultFolder(olFolderInbox): Assigns the Inbox folder to the Inbox` variable. This is where the script will look for email items.

  1. Looping Through Inbox Items:
    • For Each Item In Inbox.Items: This loop goes through each item (email) in the Inbox.
    • Inside this loop, there is another loop: For Each Atmt In Item.Attachments.
      • This inner loop iterates over each attachment (Atmt) in the current email item (Item).
  2. Processing Each Attachment:
    • FileName = SaveFolder & Atmt.FileName: Constructs the full path where the attachment will be saved. It combines the SaveFolder path with the attachment’s filename.
    • Atmt.SaveAsFile FileName: Saves the attachment to the specified FileName path.
    • i = i + 1: Increments the counter i by 1 for each attachment saved.
  3. Next Loops:
    • Next Atmt: Ends the inner loop for attachments.
    • Next Item: Ends the outer loop for email items.
  4. Completion Message:
    • MsgBox i & " Attachments saved to " & SaveFolder: Displays a message box at the end of the script execution. It shows how many attachments were saved and the location they were saved to.

Once you’ve added the VBA code to the Visual Basic Editor, you can effortlessly activate it in just a few seconds to execute the action.

How to use VBA Code in Outlook to Save email attachments automatically

Enable Macros

First, to activate macros, you need to modify some settings.

Start by going to the “File” tab, then click on “Options.”

Select “Trust Center” and proceed to click on “Trust Center Settings.”

Next, navigate to “Macro Settings” and choose the option to “Enable all macros.”

Run the Script

Press Alt + F11 to open the VBA editor, find the script, and press F5 to run it.

Running the Script

  • Open Outlook, To ensure the script is ready.
  • Press Alt + F8 to execute the action.
  • Select MarkEmailsReadOrUnread.
  • Click “Run“.

Adding VBA Code to Outlook

  1. Open VBA Editor: In Outlook, press Alt + F11 to open the VBA editor or go to the Developer tab.
  1. Click on Visual Basic to open the VBA editor.
  1. Insert a New Module: In the editor, right-click on “ThisOutlookSession” → Choose “Insert” → “Module”.
  1. Paste the Code: Copy the provided VBA code into the new module.
  2. Save the Module: Press Ctrl + S to save, and close the editor


Automating the process of saving email attachments in Outlook using VBA is a practical solution for anyone looking to enhance their email management efficiency.

This guide provides a simple yet effective script that can save you considerable time and effort.

By implementing this VBA script, you can ensure that all your important attachments are automatically saved in a designated folder, keeping your workflow organized and streamlined.

Give this method a try, and experience the ease and efficiency it brings to your daily email handling! Remember, in the realm of email management, a little automation goes a long way!

Related Articles