Fast | How to Use VBA Save Attachments in Outlook

In this article, you can learn how to use Outlook VBA to save attachments and give you a specific code. Of course, a simpler method is also recommended.

Miya

By Miya Updated on October 29, 2024

Share this: instagram reddit

What Is VBA?

VBA stands for Visual Basic for Applications. It is a programming language developed by Microsoft and is integrated into their Office suite of applications, such as Excel, Word, and PowerPoint. VBA allows users to automate tasks and create custom functionality within these applications.

VBA is primarily used for writing macros, which are sets of instructions that automate repetitive tasks or perform complex operations in Office applications. It provides a way to interact with the various objects, properties, and methods within the Office applications' object models.

With VBA, users can create custom forms, add buttons and menus, manipulate data, generate reports, interact with external data sources, and perform a wide range of other tasks. VBA code is written in the Visual Basic programming language and can be accessed and edited through the built-in VBA editor in Office applications. So how should we use VBA to save attachments in Outlook?

VBA Save Attachments in Outlook

How to Use Outlook VBA to Save Attachments

To save all attachments from Outlook via VBA, you can use the following code:

Sub SaveAttachments()
Dim objNamespace As Outlook.Namespace
Dim objInbox As Outlook.Folder
Dim objMailItem As Outlook.MailItem
Dim objAttachment As Outlook.Attachment
Dim saveFolder As String
' Set the save folder path
saveFolder = "C:\Attachments"
' Get the Outlook application and namespace
Set objNamespace = Outlook.Application.GetNamespace("MAPI")
' Get the Inbox folder
Set objInbox = objNamespace.GetDefaultFolder(olFolderInbox)
' Loop through each mail item in the Inbox
For Each objMailItem In objInbox.Items
' Check if the mail item has attachments
If objMailItem.Attachments.Count > 0 Then
' Loop through each attachment
For Each objAttachment In objMailItem.Attachments
' Save the attachment to the specified folder
objAttachment.SaveAsFile saveFolder & "\" & objAttachment.FileName
Next objAttachment
End If
Next objMailItem
' Clear memory
Set objNamespace = Nothing
Set objInbox = Nothing
Set objMailItem = Nothing
Set objAttachment = Nothing
MsgBox "Attachments saved successfully!"
End Sub

This code will save all attachments from the emails in your Outlook Inbox to the specified folder (saveFolder). You can modify the saveFolder variable to specify the desired path where you want to save the attachments. Make sure to create the folder before running the code.

☎To use this code, follow these steps:

Step 1. Open Outlook. Press Alt + F11 to open the Visual Basic Editor.

Visual Basic Editor

Step 2. In the Project Explorer window, find and double-click on the module where you want to add the code (or insert a new module).

Step 3. Copy and paste the above code into the module window.

Step 4. Modify the saveFolder variable if needed. And close the Visual Basic Editor.

Step 5. Press Alt + F8 to open the Macro dialog box. Select the "SaveAttachments" macro. Click "Run" to execute the macro.

VBA Window

The Outlook VBA will save attachments from specific folder, and a message box will appear indicating the success of the operation.

The Easiest Way to Backup Outlook and Attachments

If you are confused with the complex VBA operating system, or for some reason it didn't work out. If you don’t want to use VBA code to save attachments from Outlook, then we highly recommend AOMEI Backupper, the best backup program for automatic backup of Outlook emails and attachments.

In just a matter of minutes, AOMEI Backupper empowers you to effortlessly create a comprehensive backup of your Outlook emails and attachments. This powerful tool provides numerous advantages, making the process a breeze.

✍Complete Backup: Enjoy the peace of mind of a complete backup solution with AOMEI Backupper. It enables you to safeguard all your Outlook data, including emails, attachments, contacts, and settings, ensuring no valuable information is left behind.
✍User-Friendly: AOMEI Backupper stands apart from VBA with its user-friendly and intuitive interface, ensuring a hassle-free experience even for beginners.
✍Swift Operation: With AOMEI Backupper, your email backups and attachments are swiftly processed without any impact on your computer's performance, delivering efficiency and convenience.
✍Flexible Options: AOMEI Backupper offers flexibility in managing your backups. You can easily schedule automatic backups, compress your backups to save storage space, and select your preferred backup location as per your requirements.

To get started, simply download and install AOMEI Backupper on your computer. If you are a server user, opt for AOMEI Backupper Server. Follow these straightforward steps to back up your Outlook emails with attachments using AOMEI Backupper, and enjoy the peace of mind that comes with robust data protection.

Download Free TrialWin11/10/8.1/8/7/XP
Secure Download

Step 1. Ensure that you have logged in to your Outlook account within the Outlook application. Next, open AOMEI Backupper Professional and navigate to the Backup section. From there, select the Outlook Backup option.

Outlook Backup

Step 2. Click on the Add Outlook Data button within its interface. From the available options, choose the account that contains the Outlook emails and attachments you wish to back up. Once selected, click OK to confirm your choice.

Add Outlook Data

Step3. Click on the second checkbox, which allows you to select a storage location for your Outlook emails and attachments. You can choose a local path on your computer, a network or NAS location, or even a cloud drive.

Select Destination

Step 4. To automate the backup process for your Outlook emails and attachments, you can enable scheduled backups using daily, weekly, monthly, event triggers, and USB plug in. Simply select the desired schedule option within the backup settings. Once configured, click on the Start Backup button to initiate the automatic backup of your Outlook emails and attachments.

Schedule Backup

◉Note: In the Backup Scheme feature, you have the ability to activate automatic backup cleanup methods to address the concern of a full backup disk. By enabling this option, older backup images can be regularly deleted, ensuring efficient disk space management. This helps prevent the backup disk from becoming full and allows for smooth and uninterrupted backup operations.

Conclusion

This is all about using VBA to save attachments in Outlook. If you don't want to enter complex scripts to run, you can use professional Outlook attachment backup software - AOMEI Backupper.

This program makes it easy to backup Outlook emails with attachments. In addition to Outlook, it also supports backup of OneDrive, Dropbox, and more. It also supports previewing emails in Outlook account while restoring emails. You can also use it to backup Outlook emails to Gmail. Try it now!

Miya
Miya · Editor
Miya has an excellent insight and receives professional and systematic technical training since joining AOMEI. She has a comprehensive understanding of computer issues, aiming at helping users troubleshoot all kinds of problems. A lot of computer users around the world have found her articles very helpful!