Excel VBA code to extract all outlook global address emails and UserID

Uzma Shaheen

Active Member
Joined
Nov 10, 2012
Messages
484
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
  2. Mobile
  3. Web
Hi All,

Im trying to get outlook global email address and userId in to my workbook

What is the best way to achieve this

Please can you help me

Many many many thanks
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
I found the following in this link, I hope it helps you
https://www.mrexcel.com/forum/excel-questions/63083-global-address-list-excel.html

Code:
Sub GetOutlookAddressBook()
  Dim objOutlook As Outlook.Application, objAddressList As Outlook.AddressList
  Dim objAddressEntry As Outlook.AddressEntry, i As Long
  
  Application.ScreenUpdating = False
  Set objOutlook = CreateObject("Outlook.Application")
  Set objAddressList = objOutlook.Session.AddressLists("Global Address List")
  Sheets("Sheet1").Range("A:B").Clear
  i = 2
  For Each objAddressEntry In objAddressList.AddressEntries
    If objAddressEntry.Address <> "" Then
      Sheets("Sheet1").Cells(i, "A") = objAddressEntry.Name
      i = i + 1
    End If
  Next objAddressEntry
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thank you

How can i retrieve the alias and Name too?

So i want to get back the Name, Alias and email address

Thank you
 
Last edited:
Upvote 0
Thank you
How can i retrieve the alias and Name too?
So i want to get back the Name, Alias and email address
Thank you

Try this

Code:
Sub GetOutlookAddressBook()
  Dim objOutlook As Outlook.Application, objAddressList As Outlook.AddressList
  Dim oItem As Outlook.AddressEntry, i As Long
  Application.ScreenUpdating = False
  Set objOutlook = CreateObject("Outlook.Application")
  Set objAddressList = objOutlook.Session.AddressLists("Lista global de direcciones")
  Sheets("Sheet1").Range("A:C").ClearContents
  i = 2
  For Each oItem In objAddressList.AddressEntries
    If oItem.Address <> "" Then
      Cells(i, "A") = oItem.Name
      Cells(i, "B") = oItem.GetExchangeUser.Alias
      Cells(i, "C") = oItem.GetExchangeUser.PrimarySmtpAddress
      i = i + 1
    End If
  Next
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hi Thank you

i changed the address list to Global Address List as it could not find the way you had it in the latest post (think it was in Portuguese)

I get an object not defined or set error on the getuserexchange code for both alias and email address

ive had this issue all day and can’t figure out why
 
Upvote 0
Sorry, I forgot to translate.

Code:
[COLOR=#333333]Set objAddressList = objOutlook.Session.AddressLists("[/COLOR][COLOR=#333333]Global Address List[/COLOR][COLOR=#333333]")[/COLOR]

These are the instructions I found and they work for me.
What version of office do you have?
Hopefully someone can help.
 
Upvote 0
Thank you

I have outlook 2013 and have set a reference to outlook 15.0 library
 
Upvote 0
Add the instruction on error, it may work for some.

Code:
Sub GetOutlookAddressBook()
  Dim objOutlook As Outlook.Application, objAddressList As Outlook.AddressList
  Dim oItem As Outlook.AddressEntry, i As Long
  Application.ScreenUpdating = False
  Set objOutlook = CreateObject("Outlook.Application")
  Set objAddressList = objOutlook.Session.AddressLists("[COLOR=#0000ff]Global Address List[/COLOR]")
  Sheets("Sheet1").Range("A:C").ClearContents
  i = 2
[COLOR=#ff0000]  On Error Resume Next[/COLOR]
  For Each oItem In objAddressList.AddressEntries
    If oItem.Address <> "" Then
      Cells(i, "A") = oItem.Name
      Cells(i, "B") = oItem.GetExchangeUser.Alias
      Cells(i, "C") = oItem.GetExchangeUser.PrimarySmtpAddress
      i = i + 1
    End If
  Next
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
That actually did the trick....thank you so much

the issue was that not every one was connected in the address book hence why it threw an error
 
Upvote 0
Add the instruction on error, it may work for some.

Code:
Sub GetOutlookAddressBook()
  Dim objOutlook As Outlook.Application, objAddressList As Outlook.AddressList
  Dim oItem As Outlook.AddressEntry, i As Long
  Application.ScreenUpdating = False
  Set objOutlook = CreateObject("Outlook.Application")
  Set objAddressList = objOutlook.Session.AddressLists("[COLOR=#0000ff]Global Address List[/COLOR]")
  Sheets("Sheet1").Range("A:C").ClearContents
  i = 2
[COLOR=#ff0000]  On Error Resume Next[/COLOR]
  For Each oItem In objAddressList.AddressEntries
    If oItem.Address <> "" Then
      Cells(i, "A") = oItem.Name
      Cells(i, "B") = oItem.GetExchangeUser.Alias
      Cells(i, "C") = oItem.GetExchangeUser.PrimarySmtpAddress
      i = i + 1
    End If
  Next
  Application.ScreenUpdating = True
End Sub


Hiya

Ive ran the update and it’s taken ages to get the information. Looking like it might take over an hour to get all the info.

Can you please help me store it into an array and then I can paste into the sheet right at the end to see if I can speed it up more?

do you know any other way that I can speed it up?
Thank you
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,717
Members
448,985
Latest member
chocbudda

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top