Cannot iterate certain collections of Outlook COM objects using win32com

Viewed 33

I am seeing a similar issue to this question:

Python win32com Outlook Stores not iterable

I can write this in VBA (having included the reference to the Microsoft Outlook object library):

Dim ol As New Outlook.Application

Dim s As Outlook.Namespace

Set s = ol.GetNamespace("MAPI")

Dim ac As Outlook.Account

For Each ac In s.Accounts
    Debug.Print ac.DisplayName
Next ac

And it happily prints out the account name(s).

When I try and port to Python using this code:

import win32com.client as wc    

ol = wc.gencache.EnsureDispatch('Outlook.Application')
ns = ol.GetNamespace('MAPI')

for ac in ns.Accounts:
    print(ac.DisplayName)

I get the error message 'Accounts' object is not iterable.

If instead I write this:

for n in range(1,ns.Accounts.Count +1):
    print(ns.Accounts.Item(n).DisplayName)

the code runs as expected.

Further, if I change the object creation line to:

ol = wc.Dispatch('Outlook.Application')

and delete the gen_py files (and so forcing late binding), the iteration also works.

So, it seems to be an issue with the generated gen_py wrapper not providing the "iterability" of the Accounts object.

I also find this with the Stores collection, but not with the Folders collection (as per the other question above).

0 Answers
Related