pythonoutlookwin32comgal

How to retrieve email address from 'user.GetDirectReports()'?


Below lines are used to retrieve a given person's (by email address) line manager's email address:

import win32com.client

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
gal = outlook.Session.GetGlobalAddressList()
entries = gal.AddressEntries

chk_email = ['david@company.com']

for chk in chk_email:
    for e in entries:
        user = e.GetExchangeUser()
        if user is not None and chk == user.PrimarySmtpAddress.lower():
            print (user.GetDirectReports())

            # It prints:
            # <win32com.gen_py.Microsoft Outlook 16.0 Object Library.AddressEntries instance at 0x2115795695424>

            # then, added lines but returned nothing
            for recipient in user.GetDirectReports():
                print (recipient)                      # returns nothing
                recipient.Resolve()
                print (recipient.AddressEntry())        # returns nothing
                print (recipient.AddressEntry.Address) # returns nothing
                print (recipient.AddressEntry.GetExchangeUser.PrimarySmtpAddress()) # returns nothing

Above case David has a line manager.

I also tried another one, Nancy, with line manager also subordinates. In this line, it shows error:

recipient.Resolve()

AttributeError: '<win32com.gen_py.Microsoft Outlook 16.0 Object Library.AddressEntry instance at 0x2242384456894>' object has no attribute 'Resolve'

How can I get/interpret the line manager's email address in 'xxxx@xxxx.com' form?

I also tried user.GetExchangeUserManager()and it returns '<win32com.gen_py.None.ExchangeUser>'.


Solution

  • Of course - you get back a COM object of type AddressEntries . You need to loop through its entries.

    And never loop through all entries in GAL - call Namespace.CreateRecipient / Recipient.Resolve, then use Recipient.AddressEntry.