D
David Lingren
We are trying to build an Outlook Contacts folder from an Access database.
There are several Distribution Lists we want to add to the folder.
How can I programatically create a distribution list entry for an Outlook
Contact that has multiple e-mail addresses?
In the example below (running in Microsoft Access) "John Smith" has one
e-mail address in his Contact item and Jim Jones has two e-mail addresses in
his Contact item.
The first CreateRecipient works fine. John Smith appears in the distribution
list, his display name is "John Smith ([email protected])" and the icon to the
left of his name is the Outlook Contact busines card icon. If you
double-click on the entry the Contact form comes up.
The second CreateRecipient fails. The Resolve function returns false and
nothing is added to the list.
The third CreateRecipient succeeds, but the Display Name is "Jim Jones" and
the icon to the left of the name is the notecard that indicates a non-Contact
entry. This entry looks like it is NOT associated with an Outlook Contact.
However - if you double-click on the entry the Contact form comes up, so the
association gets made somehow. It's just that the icon and name are different.
Note that if you use the "Select Members" dialog the icon and name are set
correctly, but you can't do it from VBA. It seems a small difference, but we
want the lists to "look right".
Any ideas?
PS
Many bonus points awarded if you can tell me how to change the user profile
used in the session. The Logon and Logoff methods don't seem to work at all.
We have to manually start Outlook with the correct profile before we run the
program.
Sub AddNewMember()
'Adds a member to a new distribution list
Dim ol As New Outlook.Application
Dim oStore As Outlook.Store
Dim oRoot As Outlook.Folder
Dim oFolder As Outlook.Folder
Dim oDistListItem As Outlook.DistListItem
Dim oRecipient As Outlook.Recipient
On Error Resume Next
' ol.Session.Logoff
' ol.Session.Logon "ETaiko", , True, True
Set oStore = ol.Session.Stores("ETaiko Folders")
Set oRoot = oStore.GetRootFolder
Set oFolder = oRoot.folders("Contacts")
Set oDistListItem = oFolder.Items.Add(olDistributionListItem)
oDistListItem.DLName = "AAA Test DGroup"
'Create recipients for distlist
Set oRecipient = ol.Session.CreateRecipient("John Smith")
oRecipient.Resolve
Debug.Print "1 " & oRecipient.Resolve
oDistListItem.AddMember oRecipient
oDistListItem.Close (olSave)
Set oRecipient = ol.Session.CreateRecipient("Jim Jones")
Debug.Print "2 " & oRecipient.Resolve
oDistListItem.AddMember oRecipient
Debug.Print Err.Number & " " & Err.Description
oDistListItem.Close (olSave)
Set oRecipient = ol.Session.CreateRecipient("Jim Jones ([email protected])")
Debug.Print "3 " & oRecipient.Resolve
oDistListItem.AddMember oRecipient
Debug.Print Err.Number & " " & Err.Description
oDistListItem.Close (olSave)
End Sub
There are several Distribution Lists we want to add to the folder.
How can I programatically create a distribution list entry for an Outlook
Contact that has multiple e-mail addresses?
In the example below (running in Microsoft Access) "John Smith" has one
e-mail address in his Contact item and Jim Jones has two e-mail addresses in
his Contact item.
The first CreateRecipient works fine. John Smith appears in the distribution
list, his display name is "John Smith ([email protected])" and the icon to the
left of his name is the Outlook Contact busines card icon. If you
double-click on the entry the Contact form comes up.
The second CreateRecipient fails. The Resolve function returns false and
nothing is added to the list.
The third CreateRecipient succeeds, but the Display Name is "Jim Jones" and
the icon to the left of the name is the notecard that indicates a non-Contact
entry. This entry looks like it is NOT associated with an Outlook Contact.
However - if you double-click on the entry the Contact form comes up, so the
association gets made somehow. It's just that the icon and name are different.
Note that if you use the "Select Members" dialog the icon and name are set
correctly, but you can't do it from VBA. It seems a small difference, but we
want the lists to "look right".
Any ideas?
PS
Many bonus points awarded if you can tell me how to change the user profile
used in the session. The Logon and Logoff methods don't seem to work at all.
We have to manually start Outlook with the correct profile before we run the
program.
Sub AddNewMember()
'Adds a member to a new distribution list
Dim ol As New Outlook.Application
Dim oStore As Outlook.Store
Dim oRoot As Outlook.Folder
Dim oFolder As Outlook.Folder
Dim oDistListItem As Outlook.DistListItem
Dim oRecipient As Outlook.Recipient
On Error Resume Next
' ol.Session.Logoff
' ol.Session.Logon "ETaiko", , True, True
Set oStore = ol.Session.Stores("ETaiko Folders")
Set oRoot = oStore.GetRootFolder
Set oFolder = oRoot.folders("Contacts")
Set oDistListItem = oFolder.Items.Add(olDistributionListItem)
oDistListItem.DLName = "AAA Test DGroup"
'Create recipients for distlist
Set oRecipient = ol.Session.CreateRecipient("John Smith")
oRecipient.Resolve
Debug.Print "1 " & oRecipient.Resolve
oDistListItem.AddMember oRecipient
oDistListItem.Close (olSave)
Set oRecipient = ol.Session.CreateRecipient("Jim Jones")
Debug.Print "2 " & oRecipient.Resolve
oDistListItem.AddMember oRecipient
Debug.Print Err.Number & " " & Err.Description
oDistListItem.Close (olSave)
Set oRecipient = ol.Session.CreateRecipient("Jim Jones ([email protected])")
Debug.Print "3 " & oRecipient.Resolve
oDistListItem.AddMember oRecipient
Debug.Print Err.Number & " " & Err.Description
oDistListItem.Close (olSave)
End Sub