Home>Blogs>VBA>Download Attachments from Multiple Emails Automatically using VBA
Download attachments form multiple emails

Download Attachments from Multiple Emails Automatically using VBA

Automation has become an integral part of our everyday lives, especially in the realm of business. Among many automation tools, Visual Basic for Applications (VBA) stands out for its utility in handling routine tasks effectively. This article aims to provide a step-by-step guide on using VBA to automatically download attachments from multiple emails, with a focus on Excel files. If you’ve been looking for excel automation examples, or trying to understand excel macro automation, this guide might be just what you need.

Why Use VBA for Email Automation?

Before diving into the technicalities, it’s important to understand the advantages of using VBA for email automation.

Advantages of Using VBA for Email Automation

  1. Time Saving: Automating repetitive tasks, such as downloading email attachments, significantly saves time, allowing you to focus on more crucial tasks.
  2. Reduced Errors: Automated processes minimize human intervention, thus reducing the chance of errors.
  3. Easy to Use: If you are already using Microsoft Office, then VBA is easy to implement since it’s a built-in feature.

Easy Excel Automation: An Introduction to VBA

VBA, or Visual Basic for Applications, is a programming language developed by Microsoft that is used in many Office applications. It’s used to automate tasks within these applications and to create custom user-defined functions. Excel automation with VBA can turn tedious tasks into a single button click, saving time and reducing errors.

Download Attachments From Multiple Emails Automatically: The VBA Way

We have already written some information on how to use VBA to automate downloading attachments from multiple emails. This article will take you through this process in detail, using simple language and clear instructions.

Setting Up

Download attachments form multiple emails
Download attachments form multiple emails

First, you need to set up your worksheet correctly. Here, on cell F1, enter the path of the folder where you want to download the attachments. Columns A and B are for the ‘Email Subject’ and ‘Attachment Count’, respectively, which will fill automatically once the automation process starts.

We have also provided a button labeled “Download the attachment” to initiate the process.

The VBA Code

The VBA code for this process is provided below. Please note that you need to refer to the “Microsoft Outlook 16.0 Object Library” or the version you are using.

Microsoft Outlook 16.0 Object Library
Microsoft Outlook 16.0 Object Library
Option Explicit

Sub Get_Attachments()

Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Setting")

Dim msg As Outlook.MailItem
Dim fo As Outlook.Folder
Dim at As Outlook.Attachment

Set fo = Outlook.GetNamespace("MAPI").Folders("Your Mail Box Name Here").Folders("Inbox").Folders("My Report")

Dim lr As Integer

For Each msg In fo.Items
lr = Application.WorksheetFunction.CountA(sh.Range("A:A"))

sh.Range("A" & lr + 1).Value = msg.Subject
sh.Range("B" & lr + 1).Value = msg.Attachments.Count

For Each at In msg.Attachments
If VBA.InStr(at.Filename, ".xls") > 0 Then
at.SaveAsFile sh.Range("F1").Value & "\" & at.Filename
End If


MsgBox "Reports have been downloaded successfully"

End Sub

Opportunities for Improvement in VBA Email Automation

While VBA provides a robust framework for email automation, it’s not without room for improvement. For instance, a more user-friendly interface could make VBA more accessible to beginners. Additionally, incorporating machine learning algorithms into VBA could enable it to make predictive decisions, enhancing its automation capabilities.

Best Practices for Email Automation Using VBA

  1. Always back up your files before running any VBA script to safeguard against any unexpected issues.
  2. Avoid hardcoding variables into your VBA code, as this can cause problems when sharing the workbook or moving it to another location.
  3. Make sure your Outlook is open before starting the download process.
  4. Regularly update your VBA libraries to ensure compatibility with the latest Office updates.


Following this guide, it’s safe to say you’re well on the path to mastering the automation of downloading attachments from a bunch of emails. Whether you’re a small business on the hunt for the best email automation platform, or an individual eager to streamline your workflow, VBA unveils a vast range of opportunities to boost your productivity.

Crucially, let’s not forget that the secret to achieving successful automation doesn’t just lie in the tools you use, but also in understanding your needs. After all, the right tool in the wrong situation is the wrong tool, right? Therefore, keep an eye on your needs, pick your tools wisely, and you’ll be acing automation in no time. Happy automating, friends!

Frequently Asked Questions

Q. Can VBA download attachments from other file types, not just Excel files?

Absolutely. In fact, by making a minor adjustment in the VBA code where it checks for “.xls”, you can easily configure it to handle other types of attachments. Therefore, it’s not limited to Excel files only.

Q. Can VBA be used with other email providers, like Gmail?

While it’s technically possible to integrate VBA with Gmail through IMAP or POP3, it’s important to understand that this can be a bit more complex. The reason is, Outlook has an in-built integration with VBA, making it more straightforward. Thus, it might require extra effort to use VBA with other email providers.

Q. Do I need to have programming knowledge to use VBA?

Interestingly, you don’t necessarily need to have extensive programming knowledge to use VBA. Although having some basic understanding can be beneficial, VBA is designed to be user-friendly even for non-programmers. So, even without a programming background, you should be able to navigate the VBA environment effectively.

Visit our YouTube channel to learn step-by-step video tutorials


Watch the step-by-step video tutorial:

Meet PK, the founder of PK-AnExcelExpert.com! With over 15 years of experience in Data Visualization, Excel Automation, and dashboard creation. PK is a Microsoft Certified Professional who has a passion for all things in Excel. PK loves to explore new and innovative ways to use Excel and is always eager to share his knowledge with others. With an eye for detail and a commitment to excellence, PK has become a go-to expert in the world of Excel. Whether you're looking to create stunning visualizations or streamline your workflow with automation, PK has the skills and expertise to help you succeed. Join the many satisfied clients who have benefited from PK's services and see how he can take your Excel skills to the next level!