Search code examples
vbaoutlookcontacts

How can one update non-default mailbox contacts with a distribution list?


This is based on an example I found on the web. It creates a distribution list in the default mailbox.

I want to create a distribution list for any mailbox other than the default.

Dim oNameSpace As Outlook.NameSpace
Dim oRecipient As Outlook.Recipient

Set oNameSpace = Application.GetNamespace("MAPI")
Set oRecipient = oNameSpace.CreateRecipient("Some Person")
oRecipient.Resolve

If oRecipient.Resolved Then

    Set oItem = Application.CreateItem(olDistributionListItem)
    Set oRecipient = Application.Session.CreateRecipient("Some User")
    oRecipient.Resolve

    If oRecipient.Resolved Then
        oItem.AddMember oRecipient
        'Add note to list and display
        oItem.DLName = "Northwest -------------------"
        objItem.Body = "Regional Sales Manager - NorthWest"
        oItem.Save
   End If

End If

I want to select the mailbox the distribution list will be created in.
I want to prompt the user to confirm the correct Contacts window is open when creating the distribution list.

Dim oParent As Object

Set oParent = Application.ActiveExplorer.CurrentFolder.Parent

Resp = MsgBox("Acting on Account: " & oParent & vbCrLf & vbCrLf & "OK to Continue, Cancel to Quit", vbOKCancel)
If Resp = vbCancel Then
    End
End If

How do I resolve the below two lines?

  • Set oItem = need some code to create the distribution list in the currently selected window
  • Set oRecipient = need some code to create the recipient in the new distribution list

Solution

  • Instead of using Application.CreateItem, use MAPIFolder.Items.Add, where MAPIFolder needs to come from the Outlook store other than the default one.

    Try the following change:

    Dim oStore As Outlook.Store
    Dim oFolder As Outlook.MAPIFolder
    Set oStore = Application.ActiveExplorer.CurrentFolder.Store
    set oFolder = Store.GetDefaultFolder(olFolderContacts)
    Set oItem = oFolder.Items.Add("IPM.DistList")