Outlook VBA Save Attachments
Use this code to Automatically Save email Attachments to a Folder.
In this article, we will be getting Outlook VBA code to perform 2 tasks.
- Vba Code to save mail attachments from Outlook Folder (Inbox).
- Outlook macro to save attachments from Any Outlook Folder.
You received an interesting useful document as email attachment from your colleague or friend. You want to refer that attachment. But, it seems very hard to search them in each email in Outlook or by using the search option.
If you have Outlook macro to save attachments to a folder in your system, it would be easy to search. Here is how we can do this.
Extract Outlook Email Attachment documents to a Folder using VBA
The VBA Macro code explained in this topic will be useful when a mail folder in Outlook has lot of mail attachments and you want to extract/save all attachments to a folder.
Would that be an easy task to manually open each mail, right click & save attachment to a folder? This is where this automation would save some time.
First of all to get to access to an Attachment in Microsoft Outlook Client, we should have a basic knowledge about the object model of Outlook and how to traverse through the below listed hierarchy.
- Name Space
- Mail Folder
- Mail Item
This is the order in which we will extract Attachments from each mail item and store them in a local folder.
Related: Get Outlook Macro to Move Email from Inbox to PST Folder
VBA to Extract Outlook Mail Attachment
Name Space is used to get a reference to the Outlook Application using the command GetNameSpace (“MAPI”). Here MAPI refers to the mail protocol that is used in Outlook (Messaging API). Other Protocols commonly in use are POP and IMAP.
Once we get the instance to the Outlook Application, we drill down to desired folder (Inbox, Sent Items, Drat, Sent etc), then to the each mail present in the selected folder and then to the mail attachment.
Let’s see how it is done with the code. (This code will work fine in Outlook 2007 or 2003)
- Create a Folder “Attach” in drive D:\.
- Open Outlook Application.
- Press Alt + F11 to view the VB editor.
- Copy paste the below code to the editor.
- Execute the code by pressing F5.
Private Sub Outlook_VBA_Save_Attachment() ''Variable declarions Dim ns As NameSpace Dim inb As Folder Dim itm As MailItem Dim atch As Attachment ''Variables Initialization Set ns = Outlook.GetNamespace("MAPI") Set inb = ns.GetDefaultFolder(olFolderInbox) File_Path = "D:\attach\" ''Loop Thru Each Mail Item For Each itm In inb.Items ''Loop Thru Each Attachment For Each atch In itm.Attachments If atch.type = olByValue Then atch.SaveAsFile File_Path & atch.FileName End If Next atch Next itm '''''Notify the Termination of Process MsgBox "Attachments Extracted to: " & File_Path End Sub
Once the code is executed, all the mail attachments in the Inbox will be saved to D:\Attach Folder.
- Please make sure that drive D:\ has enough space for all the attachments, else the code will not save all attached files in mail.
- The command “On Error Resume Next” is used in the code.
- So, if there are any errors while extracting the mail attachment, the code will not notify any error to User.
- During the first run, comment this line of code and execute to know whether any error occurs during the code execution.
Save Attachments from Emails in any Outlook Folder
Here is the code to save attachment from emails from any Outlook folder. Before executing this code, please change the name of your Outlook mailbox and folder name that you want to extract the attachments.
Sub Save_Attachments_From_Emails_In_Any_Outlook_Folder() 'Declare Objects to Refer the Outlook Mailbox Dim SourceFolderRef As Outlook.MAPIFolder, SourceMailBoxName As String, Source_Pst_Folder_Name As String Dim MailItem As MailItem, MailsCount As Double, atch As Attachment, File_Path As String 'Source Mailbox or PST name File_Path = "D:\attach\" SourceMailBoxName = "YouMailId@mail.com" Source_Pst_Folder_Name = "OutlookFolderName" Set SourceFolder = Outlook.Session.Folders(SourceMailBoxName).Folders(Source_Pst_Folder_Name) 'Loop through Each Email Item in the Folder For Each MailItem In SourceFolder.Items If TypeName(MailItem) = "MailItem" Then 'Extract & Save Attachments in Email to Folder For Each atch In MailItem.Attachments If atch.Type = olByValue Then atch.SaveAsFile File_Path & atch.FileName End If Next atch End If Next MailItem 'All attachments in the Folder are processed MsgBox "Mailes in " & Source_Pst_Folder_Name & " are Processed" End Sub
If there are too many email items in the folder that you mention, then it will take longer time or a system hangup due to RAM restrictions. Please avoid executing this code suring your working hours. It is better to run this code during your lunch time or while you close all your work for the day.
Read More: How to Add and Read Outlook Calendar Appointments?
References for Outlook and Mail Attachments:
One thought on “Save Outlook Mail Attachment to Local Folder”
The below listed Post has additional topics related to saving outlook mail attachments.