I have a list Employee IDs of all the employees in my organization. I want Excel VBA code to get details like first name, last name, designation contact# and department.
The alias name is the Employee ID. So the code should take the Employee ID as alias and search Outlook for the respective details as mentioned above.
I found a macro online and modified it to my requirement:
Sub tgr()
Dim appOL As Object
Dim oGAL As Object
Dim oContact As Object
Dim oUser As Object
Dim UserIndex As Long
Dim i As Long
Dim j As Integer
Set appOL = CreateObject("Outlook.Application")
Set oGAL = appOL.GetNamespace("MAPI").AddressLists("/Name of the Distribution List/").AddressEntries
On Error Resume Next
For j = 2 To Application.WorksheetFunction.CountA(Columns(1))
For i = 1 To oGAL.Count
Set oContact = oGAL.Item(i)
If oContact.AddressEntryUserType = 0 Then
Set oUser = oContact.GetExchangeUser
If UCase(oUser.FirstName) = UCase(Range("A" & j).Value) And UCase(oUser.LastName) = UCase(Range("B" & j).Value) Then
Range("c" & j).Value = oUser.Alias
Range("D" & j).Value = oUser.JobTitle
Range("E" & j).Value = oUser.Department
Range("F" & j).Value = oUser.ManagerName
i = oGAL.Count
End If
End If
Next i
Next j
Set oGAL = Nothing
Set oContact = Nothing
Set oUser = Nothing
End Sub
The code works but the issue is that it checks all the items in the addresslist everytime to search for each item. This is taking more time.
Is there a way to simplify it by searching broadly instead of looking at each item in the addresslist and comparing. Something like Addresslist.find. Well the propety find only works if searching within a contact folder for addresslist there is no FIND property.
Global Address Listor Contacts folder accessible from Outlook or AD? You need to specify how Employee ID is related in the Contacts/AD. We would be thankful if you have tried some coding to achieve. - PatricK