Я получаю электронные письма в течение определенного периода времени из MS Outlook для заполнения листа Excel.
Использование Items.Restrict(filterstring) для ограничения результатов двумя отметками даты и времени.
Это работает для таких интервалов, как 14.06.2024 23:59 и 15.06.2024 23:59 (в течение дня), возвращая количество входящих элементов Outlook (электронные письма, приглашения на собрания, отчеты и т. д.).
Такие интервалы, как 15.06.2024 4:50 и 15.06.2024 23:59, неправильно возвращают 0 результатов, что подтверждается данными Outlook.
'Has the nescessary Outlook variable declarations and references. DateTime vars strT and EndT
restrictfilter = "[ReceivedTime] > '" & Format(CStr(strtT), "M/D/YYYY h:n") & "' AND [ReceivedTime] <= '" & Format(CStr(EndT), "M/D/YYYY h:n") & "'"
Set olItems = olFolder.Items.Restrict(restrictfilter)
allitemscount = olItems.Count


Используйте строку формата, которая возвращает 2 цифры часа, минуты, дня и месяца: "MM/DD/YYYY hh:nn"
Включите AMPM в формат.
Option Explicit
Private Sub ExtractEmail_Time()
Dim olFolder As folder
Dim olMail As MailItem
Dim i As Long
Dim restrictfilter As String
Dim olItems As Items
Dim allItemsCount As Long
Set olFolder = Session.GetDefaultFolder(olFolderInbox)
Dim strtT As Date
Dim endT As Date
strtT = "6/15/2024 4:50"
endT = "6/15/2024 23:59"
restrictfilter = "[ReceivedTime] > '" & Format(CStr(strtT), "M/D/YYYY h:n") & "' AND [ReceivedTime] <= '" & Format(CStr(endT), "M/D/YYYY h:n") & "'"
Set olItems = olFolder.Items.Restrict(restrictfilter)
allItemsCount = olItems.count
Debug.Print allItemsCount
restrictfilter = "[ReceivedTime] > '" & Format(CStr(strtT), "M/D/YYYY h:n ampm") & "' AND [ReceivedTime] <= '" & Format(CStr(endT), "M/D/YYYY h:n ampm") & "'"
Set olItems = olFolder.Items.Restrict(restrictfilter)
allItemsCount = olItems.count
Debug.Print allItemsCount
restrictfilter = "[ReceivedTime] > '" & Format(strtT, "M/D/YYYY h:n ampm") & "' AND [ReceivedTime] <= '" & Format(endT, "M/D/YYYY h:n ampm") & "'"
Set olItems = olFolder.Items.Restrict(restrictfilter)
allItemsCount = olItems.count
Debug.Print allItemsCount
restrictfilter = "[ReceivedTime] > '" & Format(strtT, "ddddd h:n ampm") & "' AND [ReceivedTime] <= '" & Format(endT, "ddddd h:n ampm") & "'"
Set olItems = olFolder.Items.Restrict(restrictfilter)
allItemsCount = olItems.count
Debug.Print allItemsCount
End Sub
Пожалуйста, проверьте значения, которые вы получаете от метода формата. Какой тип значения имеет strtT и EndT? Вы можете попробовать удалить CStr там.