I’ve created working code for filing emails by Input box for date in Outlook. However, the filter only works for the current day. Any other day before that it does not find any emails under the matching criteria.
It works by converting the inputted date (the date of reports that must be filed) into the date the emails are received (the following day). However this code only works correctly to filter emails received today. It filters them correctly, but cannot find any days previous to that.
I’ve used the 2x restrict filter option to select a date using the Microsoft advised formatting. I’ve tried adjusting the filtering so it only uses a lower bound, and I’ve checked that when removing the date filter it can still find the emails from the sender. The moment a date filter is applied to any mail received before the current day, it can no longer find the mail.
Sub EmailSort()
Dim olApp As Outlook.Application
Dim olNamespace As Outlook.NameSpace
Dim Mfolder As Outlook.MAPIFolder
Dim InboxFolder As Outlook.MAPIFolder, DestFolder As Outlook.MAPIFolder, SubFolder As Outlook.MAPIFolder
Dim olItems As Outlook.Items
Dim olMail As Outlook.MailItem
Dim sharedmailbox As Outlook.Recipient
Dim olattachment As Outlook.Attachments
Dim sendername As String, datestring As String, attachname As String
Dim i As Long, cont As Long, ii As Long
Dim sfilter As String, ssavepath As String, sdate As String, spathdate As String, yearr As String
Dim dateformat As Date
Dim NSWB As Boolean
Set olApp = Outlook.Application
Set olNamespace = olApp.GetNamespace("MAPI")
Set sharedmailbox = olNamespace.CreateRecipient("admin")
Set InboxFolder = olNamespace.GetSharedDefaultFolder(sharedmailbox, olFolderInbox)
Set DestFolder = InboxFolder.Folders("STATES")
dateformat = InputBox("Please Choose the date you'd like to sort (dd/mm/yyyy)", "Date Selector")
spathdate = Replace(dateformat, "/", "")
yearr = Right(spathdate, 2)
sdate = Replace(dateformat, "/", " ")
spathdate = Replace(spathdate, Right(spathdate, 4), yearr)
If Len(spathdate) < 6 Then sdate = "0" & sdate
If Len(spathdate) < 6 Then spathdate = "0" & spathdate
dateformat = dateformat + 1
sfilter = "[Sendername] = ""senderemail"""
sfilter = sfilter & " And [SentOn] < '" & Format(dateformat + 1, "dddd h:nn AMPM") & "'"
sfilter = sfilter & " And [SentOn] > '" & Format(dateformat, "dddd h:nn AMPM") & "'"
Set olItems = InboxFolder.Items.Restrict(sfilter)
i = olItems.Count
If olItems.Count > 0 Then