win32com Outlook滤波器按日期停止工作

发布于 2025-01-26 03:42:08 字数 1244 浏览 2 评论 0原文

我有一个脚本,显示了来自某些日期的电子邮件。它一直在工作到4月29日,现在,它无法正确过滤...

import win32com.client

import os 
from datetime import datetime, timedelta

outlook = win32com.client.Dispatch('outlook.application')
mapi = outlook.GetNamespace('MAPI')

messages = mapi.Folders("[email protected]").Folders("Inbox").Items

today = datetime.today()

start_time = today.replace(month=4,day=20, hour=0, minute=0, second=0).strftime('%Y-%m-%d %H:%M %p')
end_time = today.replace(month=5,day=2,hour=20, minute=0, second=0).strftime('%Y-%m-%d %H:%M %p')

messages = messages.Restrict("[ReceivedTime] >= '" + start_time
+ "' And [ReceivedTime] <= '" + end_time + "'")


messages.Sort("[ReceivedTime]", Descending=True)

for message in list(messages)[:5]:
    print(message.Subject, message.ReceivedTime, message.SenderEmailAddress)

我正在使用 https://www.codeforests.com/2021/05/16/python-reading-email-email-email-email-from-utlook-2/ 作为教程。

有什么想法为什么它不按预期工作? 脚本现在根本不显示任何消息。

I have a script, that shows email's from some range of dates. It was working till 29 of april, and now, it doesn't filter correctly...

import win32com.client

import os 
from datetime import datetime, timedelta

outlook = win32com.client.Dispatch('outlook.application')
mapi = outlook.GetNamespace('MAPI')

messages = mapi.Folders("[email protected]").Folders("Inbox").Items

today = datetime.today()

start_time = today.replace(month=4,day=20, hour=0, minute=0, second=0).strftime('%Y-%m-%d %H:%M %p')
end_time = today.replace(month=5,day=2,hour=20, minute=0, second=0).strftime('%Y-%m-%d %H:%M %p')

messages = messages.Restrict("[ReceivedTime] >= '" + start_time
+ "' And [ReceivedTime] <= '" + end_time + "'")


messages.Sort("[ReceivedTime]", Descending=True)

for message in list(messages)[:5]:
    print(message.Subject, message.ReceivedTime, message.SenderEmailAddress)

I was using https://www.codeforests.com/2021/05/16/python-reading-email-from-outlook-2/ as a tutorial.

Any ideas why it's not working as intended?
Script doesn't show any messages at all right now.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风透绣罗衣 2025-02-02 03:42:08

确保将日期格式正确。尽管日期和时间通常使用日期格式存储,但查找限制方法要求将日期和时间转换为字符串表示。为确保按照Microsoft Outlook的期望,将日期格式化,请使用格式函数。例如,一个vba宏显示如何使用格式函数:

sFilter = "[ReceivedTime] > '" & Format("5/02/22 3:30pm", "ddddd h:nn AMPM") & "'"

在应用过滤器之前,sort集合也很有意义。

在以下文章中阅读有关查找/findnext findnext 限制方法的更多信息:

Make sure that dates are formatted properly. Although dates and times are typically stored with a Date format, the Find and Restrict methods require that the date and time be converted to a string representation. To make sure that the date is formatted as Microsoft Outlook expects, use the Format function. For example, a VBA macro which shows how to use the Format function:

sFilter = "[ReceivedTime] > '" & Format("5/02/22 3:30pm", "ddddd h:nn AMPM") & "'"

Also it makes sense to Sort the collection before applying filters.

Read more about the Find/FindNext or Restrict methods in the following articles:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文