Download only some formats

josros60

Well-known Member
Joined
Jun 27, 2010
Messages
781
Office Version
  1. 365
Hi,

how can I modify this vba code to just download the following attachments when email is receive, format like .pdf, .xlsx, .doc and .zip format?

this is the code I have now:


VBA Code:
Public Sub saveAttachtoDiskNew(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim dateFormat
    dateFormat = Format(Now, "yyyy-mm-dd H-mm")
saveFolder = "C:\EMAIL_ATTACHMENTS"

     For Each objAtt In itm.Attachments
     
     objAtt.SaveAsFile saveFolder & "\" & dateFormat & objAtt.DisplayName
          'objAtt.SaveAsFile saveFolder & "\" & objAtt.DisplayName
          Set objAtt = Nothing
     Next
End Sub

thank you
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
I would use a helper function as shown below:

VBA Code:
Public Sub saveAttachtoDiskNew(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim dateFormat
    dateFormat = Format(Now, "yyyy-mm-dd H-mm")
    saveFolder = "C:\EMAIL_ATTACHMENTS"

     For Each objAtt In itm.Attachments
        If isMyAttachment(objAtt) Then
            objAtt.SaveAsFile saveFolder & "\" & dateFormat & objAtt.DisplayName
        End If
     Next
End Sub

Private Function isMyAttachment(objAtt As Outlook.Attachment) As Boolean
Dim arrTypes As Variant
Dim i As Integer
    arrTypes = Array("pdf", "xlsx", "doc", "zip")
    For i = 0 To UBound(arrTypes)
        If Right(objAtt.Filename, Len(arrTypes(i))) = arrTypes(i) Then
            isMyAttachment = True
            Exit Function
        End If
    Next i
End Function
 
Upvote 0
Hi. I haven't tested it yet, but this should work:

VBA Code:
Public Sub saveAttachtoDiskNew(itm As Outlook.MailItem)

    Dim objAtt As Outlook.Attachment
    Dim saveFolder As String
    Dim dateFormat As String
    
    Dim varExtension As Variant
    Dim attExtension As String
    
    
    dateFormat = Format(Now, "yyyy-mm-dd H-mm")
    saveFolder = "C:\EMAIL_ATTACHMENTS"

    For Each objAtt In itm.Attachments
        varExtension = Split(objAtt.DisplayName, ".")
        attExtension = varExtension(UBound(varExtension))
        Select Case LCase(attExtension)
            Case "pdf", "xlsx", "zip", "doc"
                objAtt.SaveAsFile saveFolder & "\" & dateFormat & objAtt.DisplayName
            Case Else
                ' Do nothing
        End Select
    Next
    Set objAtt = Nothing
End Sub
 
Upvote 0
Oops - I was too slow - that'll teach me for getting a cup of tea!
 
Upvote 0
@Dan_W - I surely like your way better than mine and it is a better answer.
I somehow never feel closer with Select Case :)

Edit: It is a better answer, mainly because (there are other reasons too, but these are the main ones):
1- It is not executing an unnecessary loop.
2- It is using LCase to make sure about the upper case file names.
 
Upvote 0
Thank you, but I agree - I never feel comfortable with Select Case - I can't explain it, it just feels like I'm being lazy or something?! Actually, my usual way of dealing with it is exactly the way you've done it (Array).
 
Upvote 0
I can't explain it
I can't explain either. I am sure it is just a habit by the way. It probably let me down in my earlier programming days by using it incorrectly, so I tried to keep away from it since then, not sure.
 
Upvote 0

Forum statistics

Threads
1,215,575
Messages
6,125,619
Members
449,238
Latest member
wcbyers

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