none
AppointmentItem.recipients.remove(index) question. RRS feed

  • Question

  • Hey Everyone,

    I've got a user using Outlook 2010 with Exchange 2010 SP1 and a privately created "public folder" chaulk full of 7500 calendar items. Outlook 2010 is producing unexpected errors when she tries to open, save, and send different items from the public calendar. From what I can tell all of the errors are related to appointment items created using outlook 2003 distribution lists. The appointments have not resolved the old distribution lists to names.

    I thought I would just loop through and remove the distribution list names and then add the individual email addresses then save the the appointment item. The add and save works fine however, I can't seem to get the remove to work correctly. I think its a problem with how I found the index value. My code is below and thank you in advance for any help you give..

    Neil

    Sub RemoveOldDistributionList()
        Dim oFolder As Outlook.MAPIFolder
        Dim Counter As Integer
        Dim ObjAppt As Outlook.AppointmentItem
        Dim ObjFolder As Outlook.Folder
        Dim ObjectProperty As Outlook.UserProperty
        
        Debug.Print (vbCrLf)
        
        Counter = 0
        Set oFolder = Application.Session.PickFolder()


        For Each ObjAppt In oFolder.Items
            Debug.Print ("List optional and required attendees: " & ObjAppt.OptionalAttendees & " : " & ObjAppt.RequiredAttendees)
            If InStr(1, ObjAppt.RequiredAttendees, "Actual Distribution List Name", vbBinaryCompare) Then
                Debug.Print ("Checking Required Attendees!")
                Dim Recipients() As String
                Dim RecipientsCounter As Integer
                RecipientsCounter = 0
                Recipients() = Split(ObjAppt.RequiredAttendees, ";", -1, vbBinaryCompare)
                Debug.Print ("Appointment has " & UBound(Recipients()) & " recipients.")
                Do While RecipientsCounter <= UBound(Recipients())
                    Debug.Print ("Current recipient is: " & Recipients(RecipientsCounter))
                    If Trim(Recipients(RecipientsCounter)) = "Actual Distribution List Name" Then
                        Debug.Print ("Found recipient to remove. " & RecipientsCounter & vbCrLf & ObjAppt.Subject)
                        ObjAppt.Recipients.Remove (RecipientsCounter)
                        'ObjAppt.Recipients.Add ("tuser@domain.com")
                        'ObjAppt.Recipients.ResolveAll
                        ObjAppt.Save
                    End If
                    RecipientsCounter = RecipientsCounter + 1
                Loop
            Else
                Debug.Print ("None")
            End If
            
            Counter = Counter + 1
        Next
        MsgBox ("Item count was " & Counter)
    End Sub 


    Monday, March 12, 2012 6:20 PM

Answers

  • Firstly, Recipients collection (just like all other OOM collections) is 1, not 0 based.

    Secondly, try to avoid multiple dot notaion - cache the ObjAppt.Recipients collection before entering the loop.

    Thirdly, you can use a down loop:

    set recipients = ObjAppt.Recipients

    for i = recipients.Count to 1 step -1

      set recipient = recipients.Item(i)

    next


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.2 is now available!

    Monday, March 12, 2012 6:28 PM

All replies

  • Firstly, Recipients collection (just like all other OOM collections) is 1, not 0 based.

    Secondly, try to avoid multiple dot notaion - cache the ObjAppt.Recipients collection before entering the loop.

    Thirdly, you can use a down loop:

    set recipients = ObjAppt.Recipients

    for i = recipients.Count to 1 step -1

      set recipient = recipients.Item(i)

    next


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.2 is now available!

    Monday, March 12, 2012 6:28 PM
  • This is collection, so you must to counting back (the same as delete rows in excel)

    as Dmitry wrote.

    Regards


    Oskar Shon, Office System MVP

    Press if Helpful; Answer when a problem solved

    Tuesday, March 13, 2012 8:28 AM