1

Is it possible to add / remove members of an Exchange Distribution List using VBA?

I can loop through members using the following code, but I can't seem to add new ones.

Dim olApp As New Outlook.Application 'Start instance of Outlook
Dim olNS As Outlook.NameSpace 'Reference the Outlook namespace and use MAPI
Set olNS = olApp.GetNamespace("MAPI") 'Open the AddressList you want to use
Dim olAddressList As Outlook.AddressList
Set olAddressList = olNS.AddressLists("Global Address List")

olAddressList.AddressEntries.Item(DLList).Members.Item (1).name
Jiminy Cricket
  • 1,377
  • 2
  • 15
  • 24

2 Answers2

2

The quick answer is no, you cannot add and remove members of an Exchange distribution group using VBA with Exchange Web Services. Distribution groups are managed on the Exchange server and the only way I know of to manage these groups with code is to use cmdlets from the Exchange Management Shell, or API to modify the Active Directory. I recently published an article on MSDN that talks about distribution groups with EWS and there is a section that lists the available cmdlets:

Managing distribution groups by using the Exchange Management Shell

Once you have created a script using these cmdlets you may be able to call the script from your VBA code using the Shell() function. I haven't done this myself but I have seen posts in other forums where people have some success with this. Here's a link to another post to get you started that shows an example of VBA in Excel calling a script:

Can I pass a VBA variable into a called Powershell script?

I hope this information helps. If this does resolve your problem, please mark this post as answered.

Thanks,

--- Bob ---

Community
  • 1
  • 1
Bob Bunn
  • 613
  • 3
  • 7
  • Thanks Bob, that does give me the answer I was looking for. Unfortunately Visual Studio isn't available to me at the moment, so I'll have to carry on doing it the manual way. On the plus side, I've written some SendKeys which will give some level of automation. – Jiminy Cricket Mar 31 '14 at 15:50
1

Bob is incorrect. You can definitely manage DL membership via VBA. I do it all the time. You manage group membership via ADSI and there is a well documented API to do so. Here are some functions I wrote to do it:

http://naterice.com/articles/62

Nathan Rice
  • 3,091
  • 1
  • 20
  • 30
  • 1
    Thanks, Nathan, I'll check this out and get back to you. If I can get this to work, you'll have made my life a lot easier!! – Jiminy Cricket Apr 02 '14 at 11:39
  • Nathan, forgive my naivety when it comes to ADSI, but where, in your code, you're refering to 'Groups' and 'Users', does this refer to 'Exchange Distribution Lists' and 'Members' respectively? I can't see any references to Outlook, Exchange or Contacts. Thanks in advance. – Jiminy Cricket Apr 03 '14 at 16:48
  • Exchange distribution lists are just AD groups, and their members are mail enabled AD users. You can add users to groups just like you do in AD to add a user to a DL. – Nathan Rice Apr 03 '14 at 16:53
  • Thanks, Nathan. I've done some reading up on ADSI, along with testing specific to my circumstances. It appears all groups accessed by your code are at the highest level. Is there a way, I wonder, to work backover from a distribution list to find its parents? This should allow me to access it via your code. Or am I looking at this completely the wrong way? – Jiminy Cricket Apr 04 '14 at 09:38
  • More online research, do I have to use LDAP? I now have the addresses of contacts and DLs – Jiminy Cricket Apr 04 '14 at 10:12
  • I'm not exactly sure what you're talking about here. You'd be wise to continue to develop on your own and repost a new question when you encounter a true roadblock with the source code you're having problems with. I hope I've been at least helpful enough for an up vote even though you've accepted bobs incorrect answer already. – Nathan Rice Apr 04 '14 at 11:39
  • Definitely an upvote there, Nathan, I will post a new question. I'll hopefully come back and mark this as resolved, shortly. – Jiminy Cricket Apr 04 '14 at 13:23