如何确定日期是否在.ReceivedTime 中?
How to determine if date is in .ReceivedTime?
我写了一个脚本来获取我自己的已发送电子邮件列表。直到最近才有效。
它适用于 2021/07 或更早的月份,但它无法接收任何 2021/08 的电子邮件。
我想这是由于某些缓存原因造成的(可能某些电子邮件还不存在于本地文件夹中)。
Sub get_Sent_mail()
On Error Resume Next
Dim olApp As Outlook.Application
Dim nmsName As NameSpace
Dim mail As mailitem
Dim text1 As String
sent_month = "2021/8"
Set olApp = Outlook.Application
Set nmsName = olApp.GetNamespace("MAPI")
For Each mail In nmsName.Folders("abc@efd.com").Folders("inbox").Folders("Sent Emails").Items
If InStr(mail.ReceivedTime, sent_month) <> 0 Then
Debug.Print mail.subject
End If
Next
End Sub
您依赖本地设置将日期时间值 mail.ReceivedTime
转换为字符串。该字符串可能包含月份名称而不是数字或带有不带前导 0 的数字。或者 year/month 序列可以不同(m/y 与 y/m)
您需要显式检索年月数并将它们视为整数
If (Month(mail.ReceivedTime) = 8) AND (Year(mail.ReceivedTime) = 2021) Then
Debug.Print mail.subject
End If
更糟糕的是,您的代码效率极低——这就像编写一个没有 WHERE 子句的 SQL 查询。你真的需要使用 Items.Restrict
:
set items = nmsName.Folders("abc@efd.com").Folders("inbox").Folders("Sent Emails").Items
set restrictedItems = items.Restrict("[ReceivedTime] >= '2021/08/01' AND [ReceivedTime] < '2021/09/01'")
for each mail in restrictedItems
Debug.Print mail.subject
next
我写了一个脚本来获取我自己的已发送电子邮件列表。直到最近才有效。
它适用于 2021/07 或更早的月份,但它无法接收任何 2021/08 的电子邮件。
我想这是由于某些缓存原因造成的(可能某些电子邮件还不存在于本地文件夹中)。
Sub get_Sent_mail()
On Error Resume Next
Dim olApp As Outlook.Application
Dim nmsName As NameSpace
Dim mail As mailitem
Dim text1 As String
sent_month = "2021/8"
Set olApp = Outlook.Application
Set nmsName = olApp.GetNamespace("MAPI")
For Each mail In nmsName.Folders("abc@efd.com").Folders("inbox").Folders("Sent Emails").Items
If InStr(mail.ReceivedTime, sent_month) <> 0 Then
Debug.Print mail.subject
End If
Next
End Sub
您依赖本地设置将日期时间值 mail.ReceivedTime
转换为字符串。该字符串可能包含月份名称而不是数字或带有不带前导 0 的数字。或者 year/month 序列可以不同(m/y 与 y/m)
您需要显式检索年月数并将它们视为整数
If (Month(mail.ReceivedTime) = 8) AND (Year(mail.ReceivedTime) = 2021) Then
Debug.Print mail.subject
End If
更糟糕的是,您的代码效率极低——这就像编写一个没有 WHERE 子句的 SQL 查询。你真的需要使用 Items.Restrict
:
set items = nmsName.Folders("abc@efd.com").Folders("inbox").Folders("Sent Emails").Items
set restrictedItems = items.Restrict("[ReceivedTime] >= '2021/08/01' AND [ReceivedTime] < '2021/09/01'")
for each mail in restrictedItems
Debug.Print mail.subject
next