Results 1 to 3 of 3

Thread: Download Attachment From Outlook Saved Mail Item Using VBA

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0

    Download Attachment From Outlook Saved Mail Item Using VBA

    hi,

    I have 150 outlook mails saved in a folder, each having an attachment. I want to download attachment from all to a folder, via VBA.
    Please Help me with that.

  2. #2
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    Here's the code

    Code:
    Sub DownloadAndSaveOutlookAttachments()
    
        Dim objFolder As Outlook.MAPIFolder
        Dim objOlMainItem As Outlook.MailItem
        Dim objOlMainItem2 As Outlook.MailItem
        Dim strFilePath As String
        Dim strTmpMsg As String
        Dim sSavePathFS As String
        Dim blnFlag As Boolean
        Dim objAtc As Attachment
        Dim objAtc2 As Attachment
        Const strSaveFolder As String = "D:\Outlook Attachment\"
    
        'path for creating attachment objOlMainItem file for stripping
        strFilePath = Environ("TEMP") & "\"
        strTmpMsg = "TemporaryMessageSave.objOlMainItem"
    
        '===============================================================================
        'If you want to specify a particular folder within the Inbox folder in Outlook, like "temp" folder, use the following code
        '===============================================================================
        'Set objFolder = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
        'Set objFolder = objFolder.Folders("Temp")
        '===============================================================================
        '===============================================================================
        
        '===============================================================================
        'To pick a folder yourself, use the following code
        Set objFolder = Application.GetNamespace("MAPI").PickFolder
        '===============================================================================
        '===============================================================================
        If objFolder Is Nothing Then Exit Sub
    
        For Each objOlMainItem In objFolder.Items
            For Each objAtc In objOlMainItem.Attachments
                blnFlag = False
                If Right$(objAtc.FileName, 3) = "objOlMainItem" Then
                    blnFlag = True
                    objAtc.SaveAsFile strFilePath & strTmpMsg
                    Set objOlMainItem2 = Application.CreateItemFromTemplate(strFilePath & strTmpMsg)
                End If
                If blnFlag Then
                    For Each objAtc2 In objOlMainItem2.Attachments
                        sSavePathFS = strSaveFolder & objAtc2.FileName
                        objAtc2.SaveAsFile sSavePathFS
                    Next objAtc2
                    objOlMainItem2.Delete
                Else
                    sSavePathFS = strSaveFolder & objAtc.FileName
                    objAtc.SaveAsFile sSavePathFS
                End If
            Next objAtc
        Next objOlMainItem
        If Len(sSavePathFS) Then
            MsgBox "Done"
        Else
            MsgBox "No attachments found"
        End If
    
    End Sub
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0
    Sorry if my question was not self explanatory.
    I have a windows folder in which 150 mails are saved as outlook messages("*.msg").
    I need to browse all and open them one by one and then download the attachment.

Similar Threads

  1. Replies: 26
    Last Post: 10-22-2019, 02:39 PM
  2. Replies: 5
    Last Post: 06-11-2013, 08:15 PM
  3. Replies: 6
    Last Post: 06-05-2013, 11:33 PM
  4. Check for Missing Attachment and Subject in Outlook
    By Transformer in forum Tips, Tricks & Downloads (No Questions)
    Replies: 0
    Last Post: 05-17-2013, 12:32 AM
  5. Replies: 4
    Last Post: 03-01-2013, 10:48 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •