Download attachments from multiple sub folders of outlook from excel.

abhay_547

Board Regular
Joined
Sep 12, 2009
Messages
179
Hi All,

I have the below macro which I got which downloads the attachments from a outlook folder which is created in Inbox .i.e. Inbox->Myfolder which means it is sub folder of Inbox but I have some sub sub folders for example Inbox->MyFolder->Abcfolder now I have such multiple subfolders from which I want to download the attachments every day. I want the macro to download only the latest files.

Code:
      Sub GetAttachments(MyMail As MailItem)
       On Error GoTo GetAttachments_err
       Dim ns As NameSpace
       Dim Inbox As MAPIFolder
       Dim SubFolder As MAPIFolder
       Dim Item As Object
       Dim Atmt As Attachment
       Dim FileName As String
       Dim i As Integer
       Set ns = GetNamespace("MAPI")
       Set Inbox = ns.GetDefaultFolder(olFolderInbox)
       Set SubFolder = Inbox.Folders("AutomationOutlook")
       i = 0
       If SubFolder.Items.Count = 0 Then
          MsgBox "There are no messages in the Inbox.", vbInformation, _
                  "Nothing Found"
          Exit Sub
       End If
        For Each Item In SubFolder.Items
          For Each Atmt In Item.Attachments
             FileName = "C:\OutlookAttachments\" & Atmt.FileName
             Atmt.SaveAsFile FileName
             i = i + 1
          Next Atmt
       Next Item
       If i > 0 Then
          MsgBox "I found " & i & " attached files." _
             & vbCrLf & "I have saved them into the C:\OutlookAttachments folder." _
             & vbCrLf & vbCrLf & "Have a nice day.", vbInformation, "Finished!"
       Else
          MsgBox "I didn't find any attached files in your mail.", vbInformation, _
          "Finished!"
      End If
      GetAttachments_exit:
         Set Atmt = Nothing
         Set Item = Nothing
         Set ns = Nothing
         Exit Sub
       GetAttachments_err:
         MsgBox "An unexpected error has occurred." _
            & vbCrLf & "Please note and report the following information." _
            & vbCrLf & "Macro Name: GetAttachments" _
            & vbCrLf & "Error Number: " & Err.Number _
            & vbCrLf & "Error Description: " & Err.Description _
            , vbCritical, "Error!"
         Resume GetAttachments_exit
          End Sub

Thanks a lot for your help in advance.:)
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Hi All,

I have found a code which had resolved my one issue .i.e. my macro now downloads only the files with specific extensions like xls, xlsx, ppt etc. Earlier it use to download all attachments but still I have following issues which are still outstanding.

1) Download the files from multiple subfolders .i.e. select the folder in tree view and use that selection in main macro to download attachments.
3) Download only the latest files.

I have selected the true for checkboxes in the property of treeview1 so that we can select the multiple folders.

I also got the below code to get the selected folders of outlook as selection for downloading the attachments from them. I need help to incorporate this so that we can fix the issue no.1

Code:
Private Sub TreeView1_NodeCheck(ByVal Node As MSComctlLib.Node)
  Dim n As Node
  
  If Node.Parent Is Nothing Then
    Set n = Node.Child
    Do Until n Is Nothing
      n.Checked = Node.Checked
      Set n = n.Next
    Loop
  End If
End Sub


I am attaching my updated macro file for your reference on the below link. please have a look.

http://www.4shared.com/file/0HTPddZQ/Save_outlook_attachments.html

Thanks a lot for your help in advance.:)
 
Last edited:
Upvote 0
Hi All,

Did anyone get the chance to look at the above post ?.


Thanks a lot for your help in advance.:)
 
Upvote 0
Hi All,

Did anyone get the chance to look at the above post ?.


Thanks a lot for your help in advance.:)
 
Upvote 0
Hi All,

Did anyone get the chance to look at the above post ?.


Thanks a lot for your help in advance.:)
 
Upvote 0
This doesn't really count as an answer but I find it easiest to use Outlook Rules. If you have a rule in place that moves emails with attachments to a single folder, then all your emails are in one place and you can run your code on that folder.
 
Upvote 0
Hi xenou,

Thanks a lot for your reply but I am already aware of the option which you have mentioned but I want a userform which will show the outlook folders and inbox in the treeview in excel (which I already have in place and have attached in my earlier post) but I need help to get the items selected in the treeview in my macro so that it can loop through all selected items / folders in outlook and download the attachments. I would request you to have a look into my main macro file once so that you get a better idea of what I am asking for and then may be you will be able to help me out with the same.


Thanks a lot for your help in advance.:)
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,262
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top