1

I am trying to find a person's email and the person's line manager. I use code below:

import win32com.client

o = win32com.client.gencache.EnsureDispatch("Outlook.Application")
ns = o.GetNamespace("MAPI")

adrLi = ns.AddressLists.Item("Global Address List")
contacts = adrLi.AddressEntries
numEntries = adrLi.AddressEntries.Count

nameAliasDict = {}

for i in contacts:
    name = i.Name
    email = i.Address
    manager = i.Manager
    alias = i.Address.split("=")[-1]
    nameAliasDict[alias] = name

    if "David" in name:                # any David's email and his manager's email address
        print (email, manager)

But it doesn't return an email address and a person's manager's email.

What would be the right way to get it? (also the other way around, knowing a person's name, to get his subordinates.)

halfer
  • 19,824
  • 17
  • 99
  • 186
Mark K
  • 8,767
  • 14
  • 58
  • 118

1 Answers1

1

You may try the following snippet:

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

for e in entries:
    user = e.GetExchangeUser()
    if user is not None:
        print(user.Name)
        print(user.FirstName)
        print(user.LastName)
        print(user.PrimarySmtpAddress)
gianpa
  • 44
  • 5
  • thank you. but error pops, AttributeError: 'NoneType' object has no attribute 'PrimarySmtpAddress' (it has no attribute 'Name', 'FirstName', 'LastName' neither) – Mark K Feb 10 '21 at 15:08
  • 1
    I have just added a control over None objects – gianpa Feb 10 '21 at 16:06
  • thank you! it works! any idea on how to get the details of person's line manager? – Mark K Feb 11 '21 at 04:21
  • 1
    The user object has such two methods user.GetDirectReports() and user.GetExchangeUserManager(). Of course pay attention to check the returned objects since they may be empty (for example if the user has no reports) – gianpa Feb 12 '21 at 10:38