How to get all items in given Outlook.Folder object which are not older than n days? RRS feed

  • Question

  • Hello

    How can I get all items in a given Outlook.Folder object which were created (not the send/receive date) in the last n days?

    I know it is possible to make it with a for loop, but handling 1000 mails is not showing a good performance and can crash.

    Best regards


    Wednesday, April 18, 2018 10:46 PM

All replies

  • Hello spinnix,

    You could try to use Items.Restrict to filter out items which are created in specific time.

    Here is the simply code.

    Sub RestrictMail()
    Dim objFolder As Folder
    Dim objResultItems As Items
    'given folder
    Set objFolder = Session.GetDefaultFolder(olFolderInbox)
    'specific days number
    n = 1
    'set filer string
    sFilter = "[CreationTime]>'" & Format(DateAdd("d", -n, Now), "ddddd h:nn AMPM") & "'"
    'filter the result
    Set objResultItems = objFolder.Items.Restrict(sFilter)
    MsgBox "There are " & objResultItems.Count & " items in the folder which are created in " & n & " days"
    End Sub

    Best Regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Thursday, April 19, 2018 1:52 AM