• Outlook User
  • New Outlook app
  • Outlook.com
  • Outlook Mac
  • Outlook & iCloud
  • Developer
  • Microsoft 365 Admin
    • Common Problems
    • Microsoft 365
    • Outlook BCM
    • Utilities & Addins

Use VBA to Create a List of Exchange GAL Members

Slipstick Systems

› Developer › Code Samples › Use VBA to Create a List of Exchange GAL Members

Last reviewed on September 10, 2022     19 Comments

How do I print a list of everyone in the Exchange Global Address List (GAL) ?

The recommended way to create a printed address list containing members of the Global Address List is to create contacts for just the people whose information you need and print it from Contacts. To do this, right-click on the entry or entries and choose Add to Contacts.

The macros on this page create an email message containing the name, alias, email address and phone number of members in an Exchange Global Address List or members of an Exchange Distribution Group. Once in Outlook, the list can be printed or copied to another program (such as Excel or Word.)
Printed Exchange GAL

Warning: It can take a long time to create a list from a large Global Address List.

List All GAL Members

Sub GetAllGALMembers()

Dim olApp As Outlook.Application
Dim olNS As Outlook.NameSpace
Dim olGAL As Outlook.AddressList
Dim olEntry As Outlook.AddressEntries
Dim olMember As Outlook.AddressEntry
Dim objMail As Outlook.MailItem

Set olApp = Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set olGAL = olNS.GetGlobalAddressList()

Set objMail = olApp.CreateItem(olMailItem)
objMail.Body = "Name" & vbTab & "Alias" & vbTab & "Email Address" & vbTab & "Business Phone" & vbCrLf
Set olEntry = olGAL.AddressEntries
On Error Resume Next
' loop through dist list and extract members
Dim i As Long
For i = 1 To olEntry.Count
  Set olMember = olEntry.Item(i)
  
  If olMember.AddressEntryUserType = olExchangeUserAddressEntry Then
  strName = olMember.Name
  strAlias = olMember.GetExchangeUser.Alias
  strAddress = olMember.GetExchangeUser.PrimarySmtpAddress
  strPhone = olMember.GetExchangeUser.BusinessTelephoneNumber
  objMail.Body = objMail.Body & strName & vbTab & " (" & strAlias & ") " & vbTab & strAddress & vbTab & strPhone & vbCrLf
  End If
Next i

objMail.Display

End Sub

 

List Members of a Distribution list

This macro lists the email address and phone numbers of the members of a specific Exchange distribution group.

Sub GetDGMembers()

Dim olApp As Outlook.Application
Dim olNS As Outlook.NameSpace
Dim olAL As Outlook.AddressList
Dim olEntry As Outlook.AddressEntry
Dim olMember As Outlook.AddressEntry
Dim lMemberCount As Long
Dim objMail As Outlook.MailItem

Set olApp = Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set olAL = olNS.AddressLists("Global Address List")

Set objMail = olApp.CreateItem(olMailItem)

' enter the list name
Set olEntry = olAL.AddressEntries("Advertiser Inquiries") 

' get count of dist list members
lMemberCount = olEntry.Members.Count

' loop through dist list and extract members
Dim i As Long
For i = 1 To lMemberCount
  Set olMember = olEntry.Members.Item(i)
  strName = olMember.Name
  strAddress = olMember.GetExchangeUser.PrimarySmtpAddress
  strPhone = olMember.GetExchangeUser.BusinessTelephoneNumber
  
  objMail.Body = objMail.Body & strName & " -- " & strAddress & " -- " & strPhone & vbCrLf
Next i

objMail.Display

End Sub

GAL to Excel

This Outlook macro writes the GAL entries to an Excel workbook. A version of this macro that runs from Excel is at "Use VBA to Export Exchange GAL to Excel".

 Private Const xlUp As Long = -4162
 Sub CopyGALToExcel()
'This is an Outlook Macro
 Dim xlApp As Object
 Dim xlWB As Object
 Dim xlSheet As Object
 Dim bXStarted As Boolean
 
 
Dim i As Long, j As Long, lastRow As Long
Dim olApp As Outlook.Application
Dim olNS As Outlook.NameSpace
Dim olGAL As Outlook.AddressList
Dim olEntry As Outlook.AddressEntries
Dim olMember As Outlook.AddressEntry

Set olApp = Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set olGAL = olNS.GetGlobalAddressList()


              
'the path of the workbook
 strPath = "d:\Documents\Book1.xlsx"
     On Error Resume Next
     Set xlApp = GetObject(, "Excel.Application")
     If Err <> 0 Then
         Application.StatusBar = "Please wait while Excel source is opened ... "
         Set xlApp = CreateObject("Excel.Application")
         bXStarted = True
     End If
     On Error GoTo 0
     'Open the workbook to input the data
     Set xlWB = xlApp.Workbooks.Open(strPath)
     Set xlSheet = xlWB.Sheets("Sheet1")

    'Find the next empty line of the worksheet
'clear all current entries
xlSheet.Cells.Select
xlApp.Selection.ClearContents

'set and format headings in the worksheet:
xlSheet.Cells(1, 1).Value = "First Name"
xlSheet.Cells(1, 2).Value = "Last Name"
xlSheet.Cells(1, 3).Value = "Phone/Ext"
xlSheet.Cells(1, 4).Value = "Email"
xlSheet.Cells(1, 5).Value = "Title"
xlSheet.Cells(1, 6).Value = "Department"

With xlSheet.Range("A1:F1")

.Font.Bold = True
.HorizontalAlignment = xlCenter

End With

Set olEntry = olGAL.AddressEntries
On Error Resume Next
'first row of entries
j = 2

' loop through dist list and extract members
For i = 1 To olEntry.Count

Set olMember = olEntry.Item(i)

If olMember.AddressEntryUserType = olExchangeUserAddressEntry Then
'add to worksheet
xlSheet.Cells(j, 1).Value = olMember.GetExchangeUser.LastName
xlSheet.Cells(j, 2).Value = olMember.GetExchangeUser.FirstName
xlSheet.Cells(j, 3).Value = olMember.GetExchangeUser.BusinessTelephoneNumber
xlSheet.Cells(j, 4).Value = olMember.GetExchangeUser.PrimarySmtpAddress
xlSheet.Cells(j, 5).Value = olMember.GetExchangeUser.JobTitle
xlSheet.Cells(j, 6).Value = olMember.GetExchangeUser.Department
j = j + 1
End If
Next i

'determine last data row, basis column B (contains Last Name):
lastRow = xlSheet.Cells(Rows.Count, "B").End(xlUp).Row

'format worksheet data area:
xlSheet.Range("A2:F" & lastRow).Sort Key1:=xlSheet.Range("B2"), Order1:=xlAscending
xlSheet.Range("A2:F" & lastRow).HorizontalAlignment = xlLeft
xlSheet.Columns("A:F").EntireColumn.AutoFit

     xlWB.Close 1
     If bXStarted Then
         xlApp.Quit
     End If
     Set xlApp = Nothing
     Set xlWB = Nothing
     Set xlSheet = Nothing
 End Sub

This table lists the fields that would be useful in these macros. See ExchangeUser members for the complete list.

Field nameDescription
AddressThe X400 e-mail address of the Exchange User
AddressEntryUserTypeReturns olExchangeUserAddressEntry which represents the user type of
the ExchangeUser
AliasThe user's alias
AssistantNameName of the user's assistant
BusinessTelephoneNumberBusiness telephone number
CityCity
CommentsComments in the GAL entry
CompanyNameThe name in the Company field
DepartmentThe department field
DisplayTypeReturns olUser from the OlDisplayType representing the nature of the
ExchangeUser
FirstNameThe user's first name
JobTitleThe job title of the user
LastNameThe last name of the ExchangeUser
MobileTelephoneNumberThe mobile telephone number
NameReturns the display name for the ExchangeUser object
OfficeLocationThe office location field
PostalCodePostal code
PrimarySmtpAddressThe primary Simple Mail Transfer Protocol (SMTP) address
StateOrProvinceState or province
StreetAddressStreet address

This table lists some of the acceptable Address Types. See OlAddressEntryUserType enumeration for the complete list.

Address TypeDescription
olExchangeUserAddressEntryAn Exchange mailbox; includes users, rooms, resources.
olExchangeDistributionListAddressEntryAn Exchange distribution list.
olExchangePublicFolderAddressEntryExchange Public Folder

How to use macros

First: You will need macro security set to low during testing.

To check your macro security in Outlook 2010 or 2013, go to File, Options, Trust Center and open Trust Center Settings, and change the Macro Settings. In Outlook 2007 and older, it’s at Tools, Macro Security.

After you test the macro and see that it works, you can either leave macro security set to low or sign the macro.

Open the VBA Editor by pressing Alt+F11 on your keyboard.

To put the code in a module:

  1. Right click on Project1 and choose Insert > Module
  2. Copy and paste the macro into the new module.

More information as well as screenshots are at How to use the VBA Editor

More Information

Extract the members of an Outlook Distribution List to a Word document (VBA Macro)
Get All Email Addresses used on Exchange Server (PowerShell cmdlet)
How to Print an Exchange Distribution List
How to Print Outlook Address Books

Use VBA to Create a List of Exchange GAL Members was last modified: September 10th, 2022 by Diane Poremsky
Post Views: 100

Share this:

  • Share on Facebook (Opens in new window) Facebook
  • Share on X (Opens in new window) X
  • Share on Reddit (Opens in new window) Reddit
  • Share on Bluesky (Opens in new window) Bluesky
  • Share on Mastodon (Opens in new window) Mastodon
  • Email a link to a friend (Opens in new window) Email

Related Posts:

  • Use VBA to Export Exchange GAL to Excel
  • How to Import Appointments into a Group Calendar
  • Create Appointments Using Spreadsheet Data
  • Use VBA to create a Mail Merge from Excel

About Diane Poremsky

A Microsoft Outlook Most Valuable Professional (MVP) since 1999, Diane is the author of several books, including Outlook 2013 Absolute Beginners Book. She also created video training CDs and online training classes for Microsoft Outlook. You can find her helping people online in Outlook Forums as well as in the Microsoft Answers and TechNet forums.

Comments

  1. Eric says

    April 12, 2023 at 2:35 am

    what is the field name for country/region?

    Reply
  2. Matt Mason says

    August 25, 2022 at 10:16 am

    Thank you for taking the time to post this. It was very helpful.

    Reply
  3. Dharm says

    September 21, 2019 at 10:25 am

    Hi,

    When I run a code to list the members of a distribution list, I get a Runtime error '-2147467259 (8004005)' at the below line:

    Set olMember = olEntry.Members.Item(i)

    What could be the problem?

    Reply
  4. aSystemOverload says

    February 10, 2017 at 5:29 am

    I'm trying to pull the members of a DG into an access table and it worked last week, but now it's not liking it half way thru, lMemberCount = olEntry.Members.Count says 53, but get to 36 and olEntry.Members.Item(36).GetExchangeUser.Name gives me 91 - Object variable or With block variable not set

    Reply
    • Diane Poremsky says

      May 30, 2017 at 1:12 pm

      Sorry i missed this earlier. Did you get it solved? Do you know what entry is #36?

      Reply
  5. Dave says

    November 18, 2016 at 9:12 am

    Great macros! I'm getting run-time error 287: application-defined or object-defined error on Set olEntry = olGAL.AddressEntries of the GetAllGALMembers subfunction. I added the MS outlook 12.0 object library. What am I missing? I'm sure it is simple. Any further help is greatly appreciated.

    Reply
  6. Pooja says

    October 18, 2016 at 3:22 pm

    How to look for details (job title, department) of a particular email address in GAL?

    Reply
    • Diane Poremsky says

      October 19, 2016 at 1:33 am

      The first macro on the pages gets all entries in the gal - the fields you can lookup are listed at the bottom of the page. The big thing is you need to look up the contact before you can get any values.

      Reply
  7. Pooja says

    October 10, 2016 at 9:14 am

    Hey! I wanted to know if there is a way to get the email sender's details directly from GAL.
    I am not able to get the details for senders who are in GAL but not in my local address book.
    Thanks.

    Reply
    • Diane Poremsky says

      October 19, 2016 at 1:32 am

      You'd need to look it in the gal using the address, then yes.

      Reply
      • Pooja says

        October 19, 2016 at 8:02 am

        Any direct method to look up other details in GAL considering email address as search keyword?

  8. Pooja says

    October 9, 2016 at 10:25 am

    My code as below:

    Public Sub DisplaySenderDetails()
    Dim Sender As Outlook.AddressEntry
    Dim xlApp As Object
    Dim xlWB As Object
    Dim xlSheet As Object
    Dim rCount As Long
    Dim bXStarted As Boolean
    Dim enviro As String
    Dim strPath As String
    Dim strColB, strColC, strColD, strColE, strColF, strColG As String
    Dim objOL As Outlook.Application
    Dim objItems As Outlook.Items
    Dim objFolder As Outlook.MAPIFolder
    Dim obj As Object
    Dim objNS As Outlook.NameSpace
    Dim olItem As Outlook.MailItem
    Dim strdate As String
    Dim oExUser As Outlook.ExchangeUser
    Dim olGAL As Outlook.AddressList
    Dim olEntry As Outlook.AddressEntries

    ' Code to set up excel

    Set objNS = GetNamespace("MAPI")
    Set olGAL = objNS.GetGlobalAddressList()
    Set objFolder = objNS.GetDefaultFolder(olFolderInbox).Folders("Abc")
    Set objItems = objFolder.Items
    Set olEntry = olGAL.AddressEntries

    For Each obj In objItems
    With obj
    Set Sender = obj.Sender
    Set olItem = obj
    If TypeName(obj) = "MailItem" Then
    On Error Resume Next
    Dim i As Long
    For i = 1 To olEntry.Count
    If olEntry.Item.Address = Sender.Address Then

    Set oExUser = Sender.GetExchangeUser
    rCount = xlSheet.Range("B" & xlSheet.Rows.Count).End(-4162).Row
    rCount = rCount + 1

    strdate = DateValue(olItem.ReceivedTime)
    If strdate >= #7/1/2016# Then
    strColB = Sender.Name
    strColC = oExUser.JobTitle
    strColD = oExUser.Department
    strColE = oExUser.PrimarySmtpAddress
    strColF = olItem.Subject
    strColG = olItem.ReceivedTime

    ' code to export the data in excel.

    Else
    Exit For
    End If
    End If
    Next i
    End If

    End With
    Next

    Set obj = Nothing
    Set objItems = Nothing
    Set objFolder = Nothing
    Set objOL = Nothing

    End Sub

    Reply
  9. Pooja says

    October 9, 2016 at 10:21 am

    Hello Diane,
    Your codes are really helpful.

    I am working on the below code. I want to extract mail and sender details from a specific folder.
    I am able to get all the details about senders in Address Book. But I am unsuccessful to get the sender's details if it is in GAL.

    For Ex. there is a sender with email address abc@xyz.com in GAL, I am not able to get job title and department of this email address as this contact is not available in my local address book.

    I am a newbie to vb and have managed to get the code in next comment.

    Thanks in advance...! :)

    Pooja

    Reply
    • Diane Poremsky says

      October 19, 2016 at 1:31 am

      This should work for those fields - it did when i added them to the first macro on this page.
      strJob = olMember.GetExchangeUser.JobTitle
      strDepartment = olMember.GetExchangeUser.Department

      Reply
  10. Mark says

    March 21, 2016 at 6:53 am

    Eventually found the property to get the home telephone number (in case anyone finds it useful)

    GetExchangeUser.PropertyAccessor.GetProperty("https://schemas.microsoft.com/mapi/proptag/0x3A09001E")

    Reply
  11. Mark says

    March 21, 2016 at 6:40 am

    Hi. Is there a way to get the home telephone number please? Can't see a property for it and Google isn't helping find an alternative method...

    Reply
  12. Stan says

    March 18, 2016 at 10:19 am

    Fantastic! That is what I was looking for, thanks

    Reply
  13. SMAZ says

    January 4, 2016 at 6:51 pm

    nice sample.
    One question...
    What if we have multiple addresses mapped on outlook client..
    How we can change to get list from GAL from a different once other than default?

    Reply
    • Diane Poremsky says

      January 5, 2016 at 12:05 am

      You'd need to identify the correct account and use it's GAL - i don't think i have any code samples that do that, but will look.

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Visit Slipstick Forums.
What's New at Slipstick.com

Latest EMO: Vol. 31 Issue 10

Subscribe to Exchange Messaging Outlook






Support Services

Do you need help setting up Outlook, moving your email to a new computer, migrating or configuring Office 365, or just need some one-on-one assistance?

Our Sponsors

CompanionLink
ReliefJet
  • Popular
  • Latest
  • Week Month All
  • Deleting Auto-Complete Entries No Longer Works
  • Use Classic Outlook, not New Outlook
  • How to Remove the Primary Account from Outlook
  • How to Hide or Delete Outlook's Default Folders
  • Removing Suggested Accounts in New Outlook
  • Disable "Always ask before opening" Dialog
  • Change Outlook's Programmatic Access Options
  • Reset the New Outlook Profile
  • Adjusting Outlook's Zoom Setting in Email
  • Understanding Outlook's Calendar patchwork colors
  • Deleting Auto-Complete Entries No Longer Works
  • Sync Issues and Errors with Gmail and Yahoo accounts
  • Error Opening iCloud Appointments in Classic Outlook
  • Opt out of Microsoft 365 Companion Apps
  • Mail Templates in Outlook for Windows (and Web)
  • Urban legend: Microsoft Deletes Old Outlook.com Messages
  • Buttons in the New Message Notifications
  • Move Deleted Items to Another Folder Automatically
  • Open Outlook Templates using PowerShell
  • Count and List Folders in Classic Outlook
Ajax spinner

Recent Bugs List

Microsoft keeps a running list of issues affecting recently released updates at Fixes or workarounds for recent issues in classic Outlook (Windows).

For new Outlook for Windows: Fixes or workarounds for recent issues in new Outlook for Windows .

Outlook for Mac Recent issues: Fixes or workarounds for recent issues in Outlook for Mac

Outlook.com Recent issues: Fixes or workarounds for recent issues on Outlook.com

Office Update History

Update history for supported Office versions is at Update history for Office

Outlook Suggestions and Feedback

Outlook Feedback covers Outlook as an email client, including Outlook Android, iOS, Mac, and Windows clients, as well as the browser extension (PWA) and Outlook on the web.

Outlook (new) Feedback. Use this for feedback and suggestions for Outlook (new).

Use Outlook.com Feedback for suggestions or feedback about Outlook.com accounts.

Other Microsoft 365 applications and services




New Outlook Articles

Deleting Auto-Complete Entries No Longer Works

Sync Issues and Errors with Gmail and Yahoo accounts

Error Opening iCloud Appointments in Classic Outlook

Opt out of Microsoft 365 Companion Apps

Mail Templates in Outlook for Windows (and Web)

Urban legend: Microsoft Deletes Old Outlook.com Messages

Buttons in the New Message Notifications

Move Deleted Items to Another Folder Automatically

Open Outlook Templates using PowerShell

Count and List Folders in Classic Outlook

Newest Code Samples

Open Outlook Templates using PowerShell

Count and List Folders in Classic Outlook

Insert Word Document into Email using VBA

Warn Before Deleting a Contact

Use PowerShell to Delete Attachments

Remove RE:, FWD:, and Other Prefixes from Subject Line

Change the Mailing Address Using PowerShell

Categorize @Mentioned Messages

Send an Email When You Open Outlook

Delete Old Calendar Events using VBA

VBA Basics

How to use the VBA Editor

Work with open item or selected item

Working with All Items in a Folder or Selected Items

VBA and non-default Outlook Folders

Backup and save your Outlook VBA macros

Get text using Left, Right, Mid, Len, InStr

Using Arrays in Outlook macros

Use RegEx to extract message text

Paste clipboard contents

Windows Folder Picker

Custom Forms

Designing Microsoft Outlook Forms

Set a custom form as default

Developer Resources

Developer Resources

Developer Tools

VBOffice.net samples

SlovakTech.com

Outlook MVP David Lee

Repair PST

Convert an OST to PST

Repair damaged PST file

Repair large PST File

Remove password from PST

Merge Two Data Files

Sync & Share Outlook Data

  • Share Calendar & Contacts
  • Synchronize two computers
  • Sync Calendar and Contacts Using Outlook.com
  • Sync Outlook & Android Devices
  • Sync Google Calendar with Outlook
  • Access Folders in Other Users Mailboxes

Diane Poremsky [Outlook MVP]

Make a donation

Mail Tools

Sending and Retrieval Tools

Mass Mail Tools

Compose Tools

Duplicate Remover Tools

Mail Tools for Outlook

Online Services

Calendar Tools

Schedule Management

Calendar Printing Tools

Calendar Reminder Tools

Calendar Dates & Data

Time and Billing Tools

Meeting Productivity Tools

Duplicate Remover Tools

Productivity

Productivity Tools

Automatic Message Processing Tools

Special Function Automatic Processing Tools

Housekeeping and Message Management

Task Tools

Project and Business Management Tools

Choosing the Folder to Save a Sent Message In

Run Rules on messages after reading

Help & Suggestions

Submit Outlook Feature Requests

Slipstick Support Services

Buy Microsoft 365 Office Software and Services

Visit Slipstick Forums.

What's New at Slipstick.com

Home | Outlook User | Exchange Administrator | Office 365 | Outlook.com | Outlook Developer
Outlook for Mac | Common Problems | Utilities & Addins | Tutorials
Outlook & iCloud Issues | Outlook Apps
EMO Archives | About Slipstick | Slipstick Forums
Submit New or Updated Outlook and Exchange Server Utilities

Send comments using our Feedback page
Copyright © 2026 Slipstick Systems. All rights reserved.
Slipstick Systems is not affiliated with Microsoft Corporation.