I've been enjoying the series of posts called What is Normalization over at Roger's Access Blog. If you get a chance, check it out, it's really great reading.
Today I've chosen to demonstrate how to programmatically create distribution lists from Excel.
I won't bore you with an explanation of what distribution lists are, and why they are useful. For that, you can check out Microsoft Outlook and Exchange Distribution Lists.
Here is a simple function that can create a simple distribution list. It accepts two arguments: a comma-separated list of email addresses, and a name for your distribution list.
We'll use some of the same elements as with other Outlook automation code: a separate function to create an object reference to the Outlook.Application Object, and a module-level boolean variable to check if we started Outlook (and close it at the end of the function).
Dim bWeStartedOutlook As Boolean
Const olSave As Long = 0
Function CreateDistList(EmailAddresses As String, DistName As String) As Boolean
' create Outlook distribution list contact from Excel
' stored in default Contacts folder
' EmailAddresses: a comma-delimited string literal list of valid email addresses
' DistName: a string literal name for the distribution list
On Error GoTo ErrorHandler
Dim vAddr As Variant
vAddr = Split(EmailAddresses, ",")
' get Outlook
Dim olApp As Object ' Outlook.Application
Set olApp = GetOutlookApp
' create distribution list items
Dim olDistListItem As Object ' Outlook.DistListItem
Dim tempMailItem As Object ' Outlook.MailItem
Dim tempRecipients As Object ' Outlook.Recipients
Set olDistListItem = olApp.CreateItem(7) ' olDistributionListItem
olDistListItem.DLName = DistName
' create dummy mailitem to pass recipient object to DistListItem
Set tempMailItem = olApp.CreateItem(0) ' olMailItem
' Outlook Object Model Guard will be triggered here
Set tempRecipients = tempMailItem.Recipients
Dim i As Long
For i = 0 To UBound(vAddr)
tempRecipients.Add(vAddr(i))
Next i
With olDistListItem
.AddMembers tempRecipients
.Close olSave
End With
' if we got this far, assume success
CreateDistList = True
GoTo ExitProc
ErrorHandler:
CreateDistList = False
ExitProc:
Set tempRecipients = Nothing
Set tempMailItem = Nothing
Set olDistListItem = Nothing
If bWeStartedOutlook Then
olApp.Quit
End If
Set olApp = Nothing
End Function
Function GetOutlookApp() As Object
On Error Resume Next
Set GetOutlookApp = GetObject(, "Outlook.Application")
If Err.Number <> 0 Then
Set GetOutlookApp = CreateObject("Outlook.Application")
bWeStartedOutlook = True
End If
On Error GoTo 0
End FunctionThe Split function returns an array, so we assign it to a Variant. The DistListItem doesn't directly accept email addresses or names; we need to pass a Recipient Object to it. So we create a temporary mailitem, add some recipients, then add those recipients to the DistListItem.
Sample usage:
Sub test() Dim success As Boolean Dim str As String str = "jsmith1@somewhere.com,jsmith2@somewhere.com" success = CreateDistList(str, "My List") End Sub
Since we're accessing the Recipients collection from outside Outlook, the OMG (Object Model Guard) will be triggered. If you use ClickYes, this shouldn't be too bothersome.
The reason we need to access the Recipients Collection is because Outlook's object model doesn't allow us to directly add members to the distribution list.
We can only add Recipient Objects, which need to be created through a MailItem. First we need to create a temporary MailItem Object, add recipients to it, then copy those recipients over to the DistListItem Object.
The code is fully late bound, so no reference to Outlook's object library is required. I've listed the Outlook constants in comments next to each line of code, if you did want to change it to early bound.
A few additional notes on the code: The first argument must be a list of email addresses, comma separated and surrounded by double quotes. The second argument is the name of the distribution list as you want it to appear (i.e. "Friends").





hi. how are you? please send me VBA Coding about update an existing distribution list.
I am also looking to update existing DL's from an excel file that is updated daily, if you could send me info concerning this process that would be amazing.
Thanks
Why don't you try the code in the article above?
I like your code but I'm having one problem and I wonder if you could advise?
The str = "jsmith1@somewhere.com,jsmith2@somewhere.com" requires quotation marks around it but I'd like to link this to a cell which has a list of emails in it (that way I can create multiple lists depending on the emails in the cell which will be generated by other code). However I'm stuck how to add this in!
If I use str = sheets("sheet name").Range("a1").value (for example) then i cannot add the quotation marks in and so the code will not work. But if I add the quotation marks it returns just the word Excel!
Any ideas?
Thanks!
If you use a cell reference, the quotes are added by Excel, you don't need to add them. For example I put this into cell A1:
jsmith1@somewhere.com,jsmith2@somewhere.com
Then I called the procedure like this:
Sub test() Dim success As Boolean Dim str As String str = ActiveSheet.Range("A1").Value success = CreateDistList(str, "My List") End SubIt worked as expected. Try it and you'll see.