Consulting

Results 1 to 5 of 5

Thread: Outlook VBA

  1. #1
    VBAX Newbie
    Joined
    Dec 2017
    Posts
    4
    Location

    Outlook VBA

    My goal is to create VBA code that adds a new member to an Exchange Distribution List.


    I've run into a number of hurdles when trying to figure this out.


    1. I have this code:


    '==============
    Dim olEntry As Outlook.AddressEntry
    Dim olEntryAdd As Outlook.AddressEntry
    Dim olDL As Outlook.ExchangeDistributionList


    Set olEntry = Application.Session.GetGlobalAddressList.AddressEntries("my address list")
    Set olDL = olEntry.GetExchangeDistributionList

    Debug.Print olDL
    Debug.Print
    Debug.Print "Members:"
    For i = 1 To olDL.Members.Count
    Debug.Print olDL.Members.Item(i), olEntry.Members.Item(i).GetExchangeUser.PrimarySmtpAddress
    Next i


    Set olEntryAdd = olEntry.Members.Add("SMTP", "John Doe", "myEmailAddress")
    '==============


    This correctly prints out each member in the list.


    However, the final line results in this error: The bookmark is not valid.


    Can someone tell me the correct syntax to add a new member to this list?


    2. Outlook Object Model


    Refer to documentation on the exchangedistributionlist object.


    The remarks discuss the AddressEntry.Members property.


    However, looking at the AddressEntry object, there is no mention of a Members property.


    Can someone point me to some documentation on this property?




    Thanks!
    Dennis

  2. #2
    The following will add a member to an existing named list or create a new named list and add the member to it if the named list doesn't exist
    Call the function as follows

    CreateDistributionList "my address list", "John Doe (myEmailAddress)"
    Private Function CreateDistributionList(strListName As String, strMember As String)
    'Graham Mayor - http://www.gmayor.com - 28/09/2016
    'strMember should be in the format "Name (e-mail address)"
    Dim olNS As Outlook.NameSpace
    Dim olFolder As Outlook.Folder
    Dim olDistList As Outlook.DistListItem
    Dim olFolderItems As Outlook.items
    Dim objRcpnt As Outlook.Recipient
    Dim X As Integer
    Dim Y As Integer
    Dim iCount As Integer
    Dim bList As Boolean
    Dim bMember As Boolean
    
        Set olNS = GetNamespace("MAPI")
        Set olFolder = olNS.GetDefaultFolder(olFolderContacts)
        Set olFolderItems = olFolder.items
        bList = False
        bMember = False
        iCount = olFolderItems.Count
        For X = 1 To iCount
            If TypeName(olFolderItems.Item(X)) = "DistListItem" Then
                Set olDistList = olFolderItems.Item(X)
                'Check if the distribution list exists
                If olDistList.DLName = strListName Then
                    bList = True
                    For Y = 1 To olDistList.MemberCount
                        'Check if the member exists
                        If InStr(1, olDistList.GetMember(Y).Name, strMember) Then
                            bMember = True
                            Exit For
                        End If
                    Next Y
                    Exit For
                End If
            End If
        Next X
        'If the distribution list doesn't exist - add it
    
        If Not bList Then
            Set olDistList = CreateItem(olDistributionListItem)
            olDistList.DLName = strListName
        End If
        'If the member doesn't exist add it
        If Not bMember Then
            Set objRcpnt = olNS.CreateRecipient(strMember)
            If objRcpnt.Resolve = True Then
                olDistList.Display
                olDistList.AddMember objRcpnt
            Else
                MsgBox strMember & vbCr & " Not Resolved"
            End If
        End If
        'Save the change to the list
        olDistList.Close olSave
    lbl_Exit:
        Set olNS = Nothing
        Set olFolder = Nothing
        Set olDistList = Nothing
        Set olFolderItems = Nothing
        Set objRcpnt = Nothing
        Exit Function
    End Function
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Newbie
    Joined
    Dec 2017
    Posts
    4
    Location
    Is DistListItem the same as ExchangeDistributionList ?

  4. #4
    Oops! Sorry I misread your message. They are not the same
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  5. #5
    VBAX Newbie
    Joined
    Dec 2017
    Posts
    4
    Location
    Is there another approach to adding a member to an Exchange Distribution List?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •