Save Outlook Attachments using VBA (Visual Basic for Applications) code

Save Outlook Attachments using VBA (Visual Basic for Applications) code

Attachment

Object refers to an attachment available in a outlook email. Attachments collection offers all attachments within a mail object, which can be use to manipulate attached items. In this article we will be writing code to save attachments for selected items in outlook to a specified location.

Code example

Public Sub SaveAttachements()
Dim objItem As Object
Dim oMail As Outlook.MailItem
'Outlook application object declaration
Dim oApp As Outlook.Application

'bind current outlook instance
Set oApp = GetObject("", "Outlook.Application")
    
Dim oExplorer As Explorer
Set oExplorer = oApp.ActiveExplorer

'Save attachement for selected items in explorer
For Each objItem In oExplorer.Selection
    Set oMail = objItem
    Dim oAttachments As Outlook.Attachments
    Set oAttachments = oMail.Attachments
    Dim oAtt As Outlook.Attachment
    For Each oAtt In oAttachments
        oAtt.SaveAsFile "C:\Users\Downloads\Blogs\msg\" & oAtt.Filename
    Next oAtt
Next objItem

'CleanUp
If Not oAtt Is Nothing Then
    Set oAtt = Nothing
End If
If Not oAttachments Is Nothing Then
    Set oAttachments = Nothing
End If
If Not oExplorer Is Nothing Then
    Set oExplorer = Nothing
End If
If Not objItem Is Nothing Then
    Set objItem = Nothing
End If
If Not oMail Is Nothing Then
    Set oMail = Nothing
End If
If Not oApp Is Nothing Then
    Set oApp = Nothing
End If
End Sub

Outlook Selection

Output

Next >> Save Outlook Emails to Local Drive or Export Outlook Emails VBA

Leave a Reply

Your email address will not be published. Required fields are marked *