none
Downloading two excel files from the same outlook email into two different folders RRS feed

  • Question

  • Hi,

    I get an email everyday with two excel files, one is .xls and other .xlsb extension. I use this code to automatically download them into a folder on my computer, and it works perfectly fine:

    Public Sub SaveAttachmentsToDisk(MItem As Outlook.MailItem)
    Dim oAttachment As Outlook.Attachment
    Dim sSaveFolder As String
    sSaveFolder = "C:\OutlookFiles"
    For Each oAttachment In MItem.Attachments
    oAttachment.SaveAsFile sSaveFolder & oAttachment.DisplayName
    Next
    End Sub

    Although, I need the files to be downloaded into separate folders.

    Could someone please help me modify this code (or suggest one) to be able to download the two kinds of files into two different folders? 

    Thanks very much for your help. 


    Friday, July 26, 2019 3:08 PM

All replies

  • Something lik this:

    Public Sub SaveAttachmentsToDisk(MItem As Outlook.MailItem)
        ' Change as needed
        Const sSaveFolder1 As String = "C:\OutlookFiles1\"
        Const sSaveFolder2 As String = "C:\OutlookFiles2\"
        Dim oAttachment As Outlook.Attachment
        For Each oAttachment In MItem.Attachments
            Select Case LCase(Right(oAttachment.FileName, 4))
                Case ".xls"
                    oAttachment.SaveAsFile sSaveFolder1 & oAttachment.DisplayName
                Case "xlsb"
                    oAttachment.SaveAsFile sSaveFolder2 & oAttachment.DisplayName
                Case Else
                    MsgBox "Attachment is neither a .xls nor a .xlsb file!", vbInformation
            End Select
        Next oAttachment
    End Sub


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Friday, July 26, 2019 3:57 PM
  • You are the best. This works!

    Thank you so much :)

    Friday, July 26, 2019 4:36 PM