Search code examples
python-3.xoutlookwin32commapi

Cannot call 'Size' property of win32com.client.Dispatch AppointmentItem after getting Outlook Appointments


I am using win32com.client to access Outlook application. I successfully managed to get the appointments from the calendar but I am interested in getting the number of appointments without getting into for loop.

I am doing the following:

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)

From this link, describing the AppointmentItem API, I found out that I can get the Size of the Outlook object. But it throws an exception

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

What am I doing wrong?

By the way, I want to use this in order to check for any appointments retrieved as a result of above query so that I do not use the restriction object on None.


Solution

  • Restrict return Items collection. It does not expose the Size property - what you need is Count.

    And if the size of the collection is not know ahead of time (Outlook calculates it on demand), use Items.GetFirst/GetNext to loop through the items in the collection.