none
Outlook VBA: managing distribution lists RRS feed

  • Question

  • I wrote a script for my own use. It runs off a rule, and for each incoming message it counts the number of recipients. If the number of recipients is less than 6 it notifies me. If the number of recipients equal 1 it also notifies me. It also moves the message to a particular folder. I obviously use this to draw my attention quickly to messages directed at me or a small group that includes me.

    ====================================================

    Sub DirectedAtMeNotify(Item As Outlook.MailItem)

    Dim myolApp As New Outlook.Application
    Dim MyNameSpace As Outlook.NameSpace
    Dim myInbox As Outlook.MAPIFolder
    Dim MyDestFolder As Outlook.MAPIFolder
    Dim myItems As Outlook.Items


    Set MyNameSpace = myolApp.GetNamespace("MAPI")
    Set myInbox = MyNameSpace.GetDefaultFolder(olFolderInbox)
    Set myItems = myInbox.Items
    Set MyDestFolder = myInbox.Folders("Direct Small Group")
     
    If Item.Recipients.Count < 6 And Item.Recipients.Count > 1 Then
    Item.Move MyDestFolder
    Item.Display
    MsgBox "You received a small group message."
    End If

    If Item.Recipients.Count = 1 Then
    Set MyDestFolder = myInbox.Folders("Direct Messages")
    Item.Move MyDestFolder
    Item.Display
    MsgBox "You received a direct message."
    End If

    End Sub

    ====================================================

     

    However, one small problem. I suspect that it may be counting a distribution list as one recipient. This would defeat the purpose of this script. Is there a way I can "count" all the members of the distributions lists, if any, before the script makes a notification?

     


    "Knowledge is the constant realization of one's ignorance."

    Monday, October 24, 2011 8:23 PM

Answers

  • Try calling this function and passing the MailItem to it:

     

    Function GetRecipCount(msg As Outlook.mailItem) As Long
      Dim msgrecips As Outlook.Recipients
      Dim i As Long
      Dim msgrecip As Outlook.Recipient
      Dim ae As Outlook.AddressEntry
      Dim count As Long
      ' get Recipients object for given msg
      Set msgrecips = msg.Recipients
      ' loop through each recipient...
      For i = 1 To msgrecips.count
        Set msgrecip = msgrecips.item(i)
        ' AddressEntry object will return count of dist list items
        Set ae = msgrecip.AddressEntry
        ' is it a distribution list? if so...
        If ae.DisplayType = olDistList Then
          ' add all members to the count
          count = count + ae.Members.count
        Else ' only a single recipient
          count = count + 1
        End If
      Next i
      ' return total to calling procedure
      GetRecipCount = count
    End Function


     


    Regards, JP
    • Marked as answer by RacerRex9727 Tuesday, November 1, 2011 3:01 PM
    Thursday, October 27, 2011 8:09 PM

All replies

  • I just made a workaround in the meantime. I created an array that includes the names of distribution lists I frequently find in the recipients. I then created a "for" loop that goes through each recipient in a message, and if it finds that recipient in the "distribution list array", it exits the sub and cancels.

     

    Not kosher, but I guess it works until I find a more robust solution.

    I just need a functionality that can identify each recipient in a message as a distribution list, or not. I can't find a method to do that though > : /



    Tuesday, October 25, 2011 12:17 AM
  • Try calling this function and passing the MailItem to it:

     

    Function GetRecipCount(msg As Outlook.mailItem) As Long
      Dim msgrecips As Outlook.Recipients
      Dim i As Long
      Dim msgrecip As Outlook.Recipient
      Dim ae As Outlook.AddressEntry
      Dim count As Long
      ' get Recipients object for given msg
      Set msgrecips = msg.Recipients
      ' loop through each recipient...
      For i = 1 To msgrecips.count
        Set msgrecip = msgrecips.item(i)
        ' AddressEntry object will return count of dist list items
        Set ae = msgrecip.AddressEntry
        ' is it a distribution list? if so...
        If ae.DisplayType = olDistList Then
          ' add all members to the count
          count = count + ae.Members.count
        Else ' only a single recipient
          count = count + 1
        End If
      Next i
      ' return total to calling procedure
      GetRecipCount = count
    End Function


     


    Regards, JP
    • Marked as answer by RacerRex9727 Tuesday, November 1, 2011 3:01 PM
    Thursday, October 27, 2011 8:09 PM
  • Thanks! I'll play with this on Tuesday when I get back to work! I need to test it on my massive DL's there.
    Sunday, October 30, 2011 10:17 PM
  • Okay, so I'm trying to call the function. Please be patient with me since I've only been dabbling in Outlook programming for a week.

     

    I try to test this function but it keeps giving me Error 424 "Object required", even though when I hover my cursor over the object variable "objitem" it yields the mailitem name.

    Sub test()

    Set objFolder = Application.Session.GetDefaultFolder(olFolderInbox).Folders("Test")
    Set objitem = objFolder.Items.Item(1)

    GetRecipCount (objitem)


    End Sub

    Tuesday, November 1, 2011 2:13 PM
  • Never mind I figured it out!
    Tuesday, November 1, 2011 3:01 PM
  • LOL, glad to hear it!
    Regards, JP
    Tuesday, November 1, 2011 5:31 PM