Search code examples
python-3.xoutlookwin32com

How can i find the email address of the "reports to" in MS Office Organization?


Problem: I have a list of email addresses of employees in my company. I want to store the email address of their boss/leader(called "Reports to" in Outlook Organization tab).

Info: I want to use Python 3.x to resolve the issue. Best would be to use win32.Dispatch('outlook.application') or similar that not require Active Directory admin intervention.

Screenshot from outlook:

enter image description here

The solution:

I found the solution after reading Eugene Astafiev's post below. Thanks a lot!

import win32com.client

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

recipient = outlook.CreateRecipient("[email protected]")
recipient.Resolve()

print ("recipient:", recipient)
print ("recipient.AddressEntry.GetExchangeUser().GetExchangeUserManager():", recipient.AddressEntry.GetExchangeUser().GetExchangeUserManager())
print ("recipient.AddressEntry.GetExchangeUser().GetExchangeUserManager().PrimarySmtpAddress:", recipient.AddressEntry.GetExchangeUser().GetExchangeUserManager().PrimarySmtpAddress)

Solution

  • You can use the GetExchangeUserManager method of the ExchangeUser class from the Outlook object model. The sequence of property and method calls looks in the following way:

    Recipient.AddressEntry.GetExchangeUser().GetExchangeUserManager()
    

    ExchangeUser is derived from the AddressEntry object, and is returned instead of an AddressEntry when the caller performs a query interface on the AddressEntry object.

    This object provides first-class access to properties applicable to Exchange users such as FirstName, JobTitle, LastName, and OfficeLocation. You can also access other properties specific to the Exchange user that are not exposed in the object model through the PropertyAccessor object. Note that some of the explicit built-in properties are read-write properties. Setting these properties requires the code to be running under an appropriate Exchange administrator account; without sufficient permissions, calling the ExchangeUser.Update method will result in a "permission denied" error.