0

Is there a way to read the Outlook user's profile information, for example job title, Department, telephone number, and reporting manager, by given an email address?

For example I have 5 email address (internal user in the same company)

I want to quickly find out above information of each email user (probably with win32com?)

Thank you.

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

2 Answers2

2

You may use the following code.

import win32com.client
import pandas as pd 
# Outlook stuff
outApp = win32com.client.gencache.EnsureDispatch("Outlook.Application")
outGAL = outApp.Session.GetGlobalAddressList()
entries = outGAL.AddressEntries
data_set = list()

# Iterates through your contact book and extracts/appends them to a list
for entry in entries:
    if entry.Type == "EX":
        user = entry.GetExchangeUser()
        if user is not None:
            if len(user.FirstName) > 0 or len(user.LastName) > 0:
                row = list()
                row.append(user.FirstName)
                row.append(user.LastName)
                row.append(user.PrimarySmtpAddress)
                row.append(user.Department)
                row.append(user.BusinessTelephoneNumber)
                row.append(user.MobileTelephoneNumber)
                row.append(user.CompanyName)
                row.append(user.Name)
                row.append(user.JobTitle)
                row.append(user.OfficeLocation)
                row.append(user.Alias)
                row.append(user.City)
                row.append(user.Comments)
                row.append(user.StateOrProvince)
                row.append(user.StreetAddress)
                data_set.append(row)
first,second,email,dept,office,mobile,company,outlook_name,title,location,alias,city,state,postal = [],[],[],[],[],[],[],[],[],[],[],[],[],[]

for ea in data_set:
    first.append(ea[0])
    second.append(ea[1])
    email.append(ea[2])
    dept.append(ea[3])
    office.append(ea[4])
    mobile.append(ea[5])
    company.append(ea[6])
    outlook_name.append(ea[7])
    title.append(ea[8])
    location.append(ea[9])
    alias.append(ea[10])
    city.append(ea[11])
    
    state.append(ea[13])
    postal.append(ea[14])
address = pd.DataFrame()

address['Full Name']= outlook_name
address['email']=email
address['Department'] = dept
address['Office No.']=office
address['Mobile No.'] = mobile
address['Company']=company
address['City'] = city
address['Title']= title
address['Office']= location
address['Alias'] = alias
address['First Name']=first
address['Last Name']=second
address['Province'] =state
address['Postal Address'] = postal
address.to_excel('Address_book with '+str(address.shape[0])+' contacts.xlsx',index=None,freeze_panes =(1,0))
Husnain
  • 65
  • 8
  • 2
    thank you! Is there a way that it reads a list of given email (instead of reading the whole address book)? – Mark K Dec 09 '21 at 03:34
  • `for email in email_list: recipient = outlook.Session.CreateRecipient(email) if recipient.Resolve(): user = recipient.AddressEntry.GetExchangeUser() if user: print(f"Name: {user.Name}, Email Address: {user.PrimarySmtpAddress}, Job Title: {user.JobTitle}, Department: {user.Department}") else:` – Chadee Fouad May 10 '23 at 04:59
0

You can use the following sequence of actions in the Outlook object model:

Recipient.AddressEntry.GetContact() 

The ContactItem class provides all the required properties you are interested in.

To get an instance of the Recipient class you need to use the CreateRecipient method of the Namespace class. For example, a sample VBA macro which shows how to use the method:

Sub ResolveName() 
 Dim myNamespace As Outlook.NameSpace
 Dim myRecipient As Outlook.Recipient
 Dim CalendarFolder As Outlook.Folder
 Set myNamespace = Application.GetNamespace("MAPI")
 Set myRecipient = myNamespace.CreateRecipient("Eugene Astafiev") ' or email address
 myRecipient.Resolve
 If myRecipient.Resolved Then 
  Call ShowCalendar(myNamespace, myRecipient) 
 End If
End Sub 

Sub ShowCalendar(myNamespace, myRecipient)
 Dim CalendarFolder As Folder 
 Set CalendarFolder = _ 
 myNamespace.GetSharedDefaultFolder _ 
 (myRecipient, olFolderCalendar) 
 CalendarFolder.Display 
End Sub
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • 1
    thank you for the answer! May I know, if there's a way to integrate above in Python codes? – Mark K May 31 '20 at 03:07