none
Are there issues with deleting items in For...Each loop? RRS feed

  • Question

  • Will the For...Each loop "loose its place" if you delete items as you come to them in the loop?
    For example deleting emails from an Outlook Email Folder as follows...
    For Each objItem in objItems
      If TypeName(objItem) = "MailItem" Then
        Set objMailItem = objItem
        objMailItem.Delete
      End If
    Next
    and since you deleted the MailItem should you Set objMailItem = nothing just before the End It?
    Thanks for any help.
    Wednesday, January 25, 2012 5:05 PM

Answers

  • 1. No. VBA is unmanaged code
     
    2. No, not within the loop where the RPC trouble would arise. That's why it's recommended to use a for loop that counts down, you can declare the object that gets set outside the loop so there's only 1 instance, and you can set it to Nothing at the end of the loop to release it or even call Marshal.ReleaseComObject() on it and run the garbage collector (GC.Collect) if needed.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "Mel_3" <=?utf-8?B?TWVsXzM=?=> wrote in message news:e93f0de8-b9f9-48fc-9d25-a186299ef4ee...

    1 - Does VBA use .Net?

    2 - Any way to clear the enumerator?

    thanks

     


    Ken Slovak MVP - Outlook
    Wednesday, January 25, 2012 7:05 PM
    Moderator

All replies

  • Yes, you have to loop backwards:

    Dim intX As Integer
    
    For intX = objItems.Count To 1 Step -1
        Set objMailItem = objItems(intX)
        objMailItem.Delete
    Next
    


    Eric Legault
    MVP (Outlook)
    About me...
    Wednesday, January 25, 2012 5:09 PM
    Moderator
  • Another problem with foreach loops in .Net is that the items will be kept referenced by the enumerator, potentially causing Outlook to run out of RPC channels in case of online Exchange mode.
    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.2 is now available!
    Wednesday, January 25, 2012 5:23 PM
  • 1 - Does VBA use .Net?

    2 - Any way to clear the enumerator?

    thanks

     

    Wednesday, January 25, 2012 6:47 PM
  • 1. No. VBA is unmanaged code
     
    2. No, not within the loop where the RPC trouble would arise. That's why it's recommended to use a for loop that counts down, you can declare the object that gets set outside the loop so there's only 1 instance, and you can set it to Nothing at the end of the loop to release it or even call Marshal.ReleaseComObject() on it and run the garbage collector (GC.Collect) if needed.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "Mel_3" <=?utf-8?B?TWVsXzM=?=> wrote in message news:e93f0de8-b9f9-48fc-9d25-a186299ef4ee...

    1 - Does VBA use .Net?

    2 - Any way to clear the enumerator?

    thanks

     


    Ken Slovak MVP - Outlook
    Wednesday, January 25, 2012 7:05 PM
    Moderator