3

I'm trying to write a code to download weekly assignments (attachments) and save it to a folder.

I got a code which goes through every item and downloads all the attachments but it goes from latest to earliest date. I need the latest one as the earlier attachments will overwrite the later ones.

I added a restrict method to look for items that are sent today but it still goes through the whole inbox.

Sub downloadAttachment()

Dim ns As NameSpace
Dim Inbox As MAPIFolder
Dim Item As Object
Dim myItems As Items
Dim Atmt As Attachment
Dim FileName As String
Dim i As Integer
Dim sFilter As String


'Setting variable for inbox.
Set ns = GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
**sFilter = "[ReceivedTime]>=""&Date()12:00am&"""
Set myItems = Inbox.Items.Restrict(sFilter)**

i = 0

'Error handling.
On Error GoTo downloadattachment_err

'if no attachments, msgbox displays.
If Inbox.Items.Count = 0 Then
    MsgBox "There are no messages in the Inbox.", vbInformation, _
            "Nothing Found"
    Exit Sub
End If

'Goes through each item in inbox for attachments.
For Each Item In Inbox.Items
    For Each Atmt In Item.Attachments
    If Right(Atmt.FileName, 3) = "txt" Then
        FileName = "C:\losscontroldbases\pendingworkdownload\" & Atmt.FileName
        Atmt.SaveAsFile FileName
        i = i + 1
    End If
    Next Atmt
Next Item

'If attachments found, the displays message.
If i > 0 Then
    MsgBox "I found " & i & " attached files." _
    & vbCrLf & "I have saved them into the C:\losscontroldbases\pendingworkdownload." _
    & vbCrLf & "Have a nice day!"

Else
    MsgBox "I didn't find any attached files in your mail."
End If

'Clearing memory.
downloadattachment_exit:
    Set Atmt = Nothing
    Set Item = Nothing
    Set ns = Nothing
    Exit Sub

'Error handling code.
downloadattachment_err:
    MsgBox " An unexpected error has occured."

End Sub
2
  • I fixed it already. Thank you for asking!
    – Nigel
    Sep 26, 2016 at 19:49
  • Please remember to mark the answer, so we know its solved
    – 0m3r
    Sep 26, 2016 at 20:08

1 Answer 1

3

Your code references "date" string as a literal value. Use something like

Filter = "[ReceivedTime]>= '" & CStr(Date()) & " 12:00am' "
4
  • Hello, how would I make the Filter set to todays current date?
    – Lynn
    Sep 28, 2020 at 1:12
  • 1
    You woudl need to have two parts: >= midnight today and < then tomorrow midnight Sep 29, 2020 at 5:29
  • Ok thank you, there’s no datetime.today() function? I am still researching.
    – Lynn
    Sep 29, 2020 at 7:15
  • 1
    No. DateTime in COM is an 8 byte float (double) that stores both date and time. What you are asking is a range (midnight to midnight) - you cannot use = with DateTime fields, it must always be a range. Sep 29, 2020 at 15:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.