PDA

View Full Version : Adding Members to Outlook Distribution Lists? (WXP-Pro)



msearly
05-05-2004, 11:12 AM
Is there any reason why this code should not work?

Public Function AddMemberToDL(sDlName As String, sEntryName As String, sEmailAddress As String) As Boolean
On Error GoTo err_AddMemToDL
Dim oContacts As Outlook.MAPIFolder
Dim oDList As Outlook.DistListItem
Dim oRecipient As Outlook.Recipient
Dim oMailItem As Outlook.MailItem

AddMemberToDL = True

'I log onto Outlook and establish the value of olNS elsewhere in my program. (olNS = Outlook.NameSpace).

Set oContacts = olNs.GetDefaultFolder(olFolderContacts)
Set oDList = oContacts.Items(sDlName)
Set oMailItem = olApp.CreateItem(olMailItem)
Set oRecipient = oMailItem.Recipients.Add(sEntryName)
With oRecipient
.AddressEntry.Type = "SMTP"
.AddressEntry.Address = sEmailAddress
.Resolve
End With

oDList.AddMembers oMailItem.Recipients

'If I do a "oDList.show" right here, it shows me the distribution list with the entry in it, but I have to click on the Update button to get the list to save the entry. This would be fine if I only had a user or two to add, but I have nearly a thousand to do. How do I progmatically do what clicking on the update button in Outlook does?

oDList.Save

Exit Function
err_AddMemToDL:
AddMemberToDL = False
MsgBox "Error adding recipient to DL List." & vbLf & "Entry Name: " & sEntryName & vbLf & "DL: " & sDlName & vbLf & err.Description
End Function

This function returns no errors but it does not save the entry in the distribution list.

Thanks



Matt Early
Network Administrator
Prince Edward County Public Schools
Farmville, VA

dineshcooper
05-10-2004, 04:18 AM
Hi

There is an article at:
http://support.microsoft.com/?kbid=269861

with the header:
OL2000: (IMO) You Cannot Programmatically Add an E-mail Address to a Distribution List

that states:
"The AddMembers method of a Distribution List item does not work unless the e-mail address can be resolved to a Contact item. "

A script is supplied that may answer your problem:

Sub CreateDistributionList()

Dim myOlApp As Application
Dim myNameSpace As NameSpace
Dim myContact As ContactItem
Dim myDistList As DistListItem
Dim myMailItem As MailItem
Dim myRecipients As Recipients

Set myOlApp = CreateObject("Outlook.Application")
Set myNameSpace = myOlApp.GetNamespace("MAPI")

'Create the new Contact
Set myContact = myOlApp.CreateItem(olContactItem)
myContact.FullName = "User Name"
myContact.Email1Address = "username@domain.com"
myContact.Save
myContact.Display

'Create the Distribution List item
Set myDistList = myOlApp.CreateItem(olDistributionListItem)
myDistList.DLName = "Test Distribution List"

'The MailItem is required to
'create the Recipients collection
Set myMailItem = myOlApp.CreateItem(olMailItem)
Set myRecipients = myMailItem.Recipients

'A Contact with the following e-mail address
'must exist for the AddMembers method to work
myRecipients.Add "username@domain.com"
myRecipients.ResolveAll
myDistList.AddMembers myRecipients
myDistList.Save
myDistList.Display

End Sub


Hope that helps