按时间范围检索Outlook电子邮件



我正在尝试从Outlook收件箱中获取电子邮件,但已按时间范围过滤(例如,下午4点至上午8点之间收到的电子邮件(。我从下面的代码开始,但不知道如何完成。我将此文档用于Outlook API。

import win32com.client as client
# startup outlook instance
outlook = client.Dispatch('Outlook.Application')
# get namespace so that we can access folders
namespace = outlook.GetNameSpace('MAPI')
account = namespace.Folders['MyAccountName']
# get the inbox folder, specifically
inbox = account.Folders['Inbox']
Filter = ("[SentOn] > 'WhatToPlaceHereAsAStartHour???' AND "
"[SentOn] < 'WhatToPlaceHereAsAEndHour???'")
# I tried somethin like '16:00' and '8:00' but didnt work...
Items = inbox.Items.Restrict(Filter)
Item = Items.GetFirst()
print(Item.subject)

日期时间值必须包括日期时间,例如"01/09/2022 16:00"

尽管日期和时间通常以Date格式存储,但FindRestrict方法要求将日期和时间转换为字符串表示。例如,为了确保日期的格式符合Microsoft Outlook的要求,可以使用VBA中提供的Format函数。以下示例创建了一个过滤器,用于查找2020年1月15日下午3:30 之后修改的所有联系人

sFilter = "[LastModificationTime] > '" & Format("1/15/2020 3:30pm", "ddddd h:nn AMPM") & "'"

有关Restrict方法的详细信息,请参阅How To:Use Restrict方法从文件夹文章中检索Outlook邮件项目。

最新更新