筛选当前日期(今天)发送的Outlook电子邮件

时间:2020-09-28 04:49:09

标签: python vba email outlook automation

我想下载来自Outlook收件箱的特定附件。我想根据主题和发送日期过滤这些电子邮件。

这就是我在做什么:

 Outlook = win32com.client.Dispatch("Outlook.Application")
 olNs = Outlook.GetNamespace("MAPI")
 Inbox = olNs.GetDefaultFolder(6)

 Filter = "[Subject] = 'Important Report' And [SenderEmailAddress] = 'data@cool.com' And [SentOn] 
 > '9/26/2020 08:00 AM'"


 Items = Inbox.Items.Restrict(Filter)
 Item = Items.GetFirst()

 for attachment in Item.Attachments:
 print(attachment.FileName)
 attachment.SaveAsFile(r"C:\Users\lynnette\Desktop\file.xls")

当前,它是使用[SentOn]进行过滤的,但是,我使用的是特定日期。我想根据当前日期进行过滤。我已经研究了Items.Restrict,找不到当前日期过滤器。

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

我相信您想执行以下三个操作之一:

今天:

Items.Restrict("@SQL=%today(\"urn:schemas:httpmail:datereceived\")%")

从确切日期开始:

Items.Restrict("@SQL=(\"urn:schemas:httpmail:datereceived\" >= '9/26/2020 00:00')")

日期之间:

Items.Restrict("@SQL=(\"urn:schemas:httpmail:datereceived\" >= '9/26/2020 00:00' AND \"urn:schemas:httpmail:datereceived\" <= '9/27/2020 23:59')

Microsoft documentation

更新1:

在您的代码中,您可以只使用第二次限制。

Items = Inbox.Items.Restrict(Filter)
Items = Items.Restrict("@SQL=%today(\"urn:schemas:httpmail:datereceived\")%")
Item = Items.GetFirst()