For Each loop: Just deletes the very first attachment

前端 未结 2 1358
独厮守ぢ
独厮守ぢ 2021-01-28 06:47

I\'ve been trying to delete the attachments in Outlook after copying them using for each loop. It just deletes the very first attachment after copying it but does not go for the

2条回答
  •  时光说笑
    2021-01-28 07:46

    In your previous question we changed from an indexed loop to a non-indexed loop, because you did not have any .Delete requirement. Unfortunately, deleting items from a collection requires an indexed iteration.

    This is because, when you have 3 items:

    • Item 1 = Attachment 1
    • Item 2 = Attachment 2
    • Item 3 = Attachment 3

    Then when you delete the first item (Item 1/Attachment 1), it takes you to item 2, but when the delete happens, you are left with the collection that looks like:

    • Item 1 = Attachment 2
    • Item 2 = Attachment 3

    So your loop will delete items 1 and 3, but it will never touch item 2.

    The simplest way to fix this for you, without using an indexed loop and re-writing your script, is to just add another loop to do the delete method.

    @Enderland provides the example for this. I will not duplicate his effort, but I did want to explain what is happening for you. This is always the case when deleting items from a collection, you have to step through the collection in reverse order.

提交回复
热议问题