首页 > 解决方案 > 获取 Outlook 约会后无法调用 win32com.client.Dispatch AppointmentItem 的“大小”属性

问题描述

我正在使用win32com.client访问 Outlook 应用程序。我成功地从日历中获得了约会,但我有兴趣在不进入for循环的情况下获得约会的数量。

我正在执行以下操作:

import win32com.client
outlook = win32com.client.Dispatch("Outlook.Application")
namespace = outlook.GetNamespace('MAPI')

appointments = namespace.GetDefaultFolder(9).Items

appointments.Sort("[Start]")
appointments.IncludeRecurrences = "True"

restriction = "[Start] >= '" + start_date.strftime('%Y.%m.%d') + "' AND [Start] <= '" + \
              end_date.strftime('%Y.%m.%d') + "'"
restricted_items = appointments.Restrict(restriction)
print(restricted_items.Size)

这个描述 AppointmentItem API 的链接中,我发现我可以获取 Outlook 对象的大小。但它抛出一个异常

AttributeError: '<win32com.gen_py.Microsoft Outlook 15.0 Object Library._Items instance at 0x73837256>' object has no attribute 'size'

我究竟做错了什么?

顺便说一句,我想使用它来检查由于上述查询而检索到的任何约会,这样我就不会restrictionNone.

标签: python-3.xoutlookwin32commapi

解决方案


Restrict返回项目集合。它不公开 Size 属性-您需要的是Count.

如果事先不知道集合的大小(Outlook 会按需计算),请使用Items.GetFirst/GetNext循环遍历集合中的项目。


推荐阅读