Nostromo
Nostromo

Reputation: 1264

How to get the members of an Outlook DistributionList as fast as possible?

I need to get the e-mail addresses of the members of an Outlook distribution list. This list is not in my local folder, it's a company wide list.

I have a solution, but it needs a substantial amount of time, because I iterate through all existing groups (8,400+) to find the group in question, before handling the members of this group.

Is there a better way to get the members of a distribution list, if I already know its name (no need to get all the other 8,400+ distribution lists as well)?

Here's my code:

outlookApp = New Outlook.Application
outlookNamespace = _outlookApp.GetNamespace("mapi")

Dim addrLists As Outlook.AddressLists =
    outlookApp.Session.AddressLists
Dim root As Outlook.AddressList =
    addrLists.OfType(Of Outlook.AddressList) _
             .FirstOrDefault(Function(x) x.Name = "All Groups")
Dim entry As Outlook.AddressEntry =
    root.AddressEntries.Cast(Of Outlook.AddressEntry) _
                       .FirstOrDefault(Function(x) x.Name = "<Enter Group Name Here>")

entry.Members contains all the members of this list.


I tried to use the function GetAddressEntryFromID but I only get an error "Unfortunately a problem occured. You can try again." (translated from german).

Upvotes: 0

Views: 282

Answers (2)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66215

It is a lot simpler than that:

set allGroups = Application.Session.AddressLists.Item("All Groups")
set addressEntry = allGroups.AddressEntries.Item("<Enter Group Name Here>")
for each m in addressEntry.Members
  MsgBox m.Name
next

Upvotes: 1

Nostromo
Nostromo

Reputation: 1264

I found it myself.

Dim rec As Outlook.Recipient =
    outlookNamespace.CreateRecipient("<Enter Group Name Here>")
rec.Resolve

rec.AddressEntry.Members contains all the members of the list.

Upvotes: 0

Related Questions