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.
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)
Visual Basic1234567891011121314151617181920212223242526Private Sub Outlook_VBA_Save_Attachment()''Variable declarionsDim ns As NameSpaceDim inb As FolderDim itm As MailItemDim atch As Attachment''Variables InitializationSet ns = Outlook.GetNamespace("MAPI")Set inb = ns.GetDefaultFolder(olFolderInbox)File_Path = "D:\attach\"''Loop Thru Each Mail ItemFor Each itm In inb.Items''Loop Thru Each AttachmentFor Each atch In itm.AttachmentsIf atch.type = olByValue Thenatch.SaveAsFile File_Path & atch.FileNameEnd IfNext atchNext itm'''''Notify the Termination of ProcessMsgBox "Attachments Extracted to: " & File_PathEnd Sub
- 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.
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.Outlook VBA Save Attachments to FolderVisual Basic123456789101112131415161718192021222324252627Sub Save_Attachments_From_Emails_In_Any_Outlook_Folder()'Declare Objects to Refer the Outlook MailboxDim SourceFolderRef As Outlook.MAPIFolder, SourceMailBoxName As String, Source_Pst_Folder_Name As StringDim MailItem As MailItem, MailsCount As Double, atch As Attachment, File_Path As String'Source Mailbox or PST nameFile_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 FolderFor Each MailItem In SourceFolder.ItemsIf TypeName(MailItem) = "MailItem" Then'Extract & Save Attachments in Email to FolderFor Each atch In MailItem.AttachmentsIf atch.Type = olByValue Thenatch.SaveAsFile File_Path & atch.FileNameEnd IfNext atchEnd IfNext MailItem'All attachments in the Folder are processedMsgBox "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.
References for Outlook and Mail Attachments:
Save Outlook Mail Attachment to Local Folder