0

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>'.

Community
  • 1
  • 1
Mark K
  • 8,767
  • 14
  • 58
  • 118

1 Answers1

2

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.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • thank you for your reply. I added lines: 1.for recipient in user.GetDirectReports(): 2. recipient.Resolve(), 3.print(recipient.AddressEntry()). But it returns nothing. Am I using them right? (question updated with above). thanks again. – Mark K Feb 19 '21 at 09:27
  • Once again, you are treating a COM object (such as as ExchangeUser or AddressEntry) like a scalar type (string or int). Print(SomeComObject) is meaningless. You need to actually read a scalar property, e.g. print(recipient.AddressEntry.Address) – Dmitry Streblechenko Feb 19 '21 at 17:57
  • thanks again for your reply. I don't know what's going on: from "for recipient in user.GetDirectReports():", I tried different prints (updated in the question), all returned nothing. – Mark K Feb 22 '21 at 14:01
  • 1
    "Nothing" as in "no items in the collection"? Or "nothing gets printed"? – Dmitry Streblechenko Feb 23 '21 at 15:19
  • thanks again. "Nothing" as no output is printed, i.e. when the script ran finished, it's finished, not a single line of result. – Mark K Feb 24 '21 at 01:19
  • I've added to the question, and marked those lines with "# returns nothing". – Mark K Feb 24 '21 at 04:59
  • 1
    Ca you step through the code? Is it possible GetDirectReports returns 0 items for that user? – Dmitry Streblechenko Feb 24 '21 at 16:48
  • ,I did run the lines one-by-one. When "print (user.GetDirectReports())", it prints "". The case David has a line manager. I tried another another one, Nancy, with line manager also subordinates. Strangely in the "recipient.Resolve()", it shows error: ...object has no attribute 'Resolve'. – Mark K Feb 25 '21 at 14:18
  • 1
    Once again, printing a COM object is meaningless, you need to deal with scalar object properties. .GetDirectReports retruns AddressEntires collection, whcih contain AddressEntry objects, not Recipient. And AddressEntry object does not expose the Resolve method - see MSDN. – Dmitry Streblechenko Feb 25 '21 at 20:15
  • Tthank you for your help all along. I am still not able to have a code running and successful in the queries. I will figure out other non Python way. So closing this question. – Mark K Mar 10 '21 at 13:49