For Each loop: Not deleting all emails

前端 未结 3 1479
忘掉有多难
忘掉有多难 2021-01-28 02:01

I wrote the below in an attempt to save emails older than six months in an external folder:

Option Explicit

Public Sub EBS()
Dim oMail As MailItem
Dim sPath As          


        
相关标签:
3条回答
  • 2021-01-28 02:14

    Instead of iterating over all items in the folder and checking the following condition:

    If oMail.ReceivedTime < DateAdd("d", -180, Now) Then
    

    You can find the required items and iterate over a subset of items that correspond to your conditions.

    See How To: Use Find and FindNext methods to retrieve Outlook mail items from a folder (C#, VB.NET) for a sample code. There you can find a similar article related to the Restrict method (can't post more than one link).

    0 讨论(0)
  • 2021-01-28 02:19

    You would also want to use Items.Find/FindNext or Items.Restrict instead of looping through all items in a folder.

    UPDATE:

    setItems = oInboxFolder.Items
    set RestrictedItems = setItems.Restrict(" ([ReceivedTime ] < '05/02/2014')) AND ([MessageClass] = 'IPM.Note' ")
    for I = RestrictedItems.Count to 1 step -1 do
      Set oMail = RestrictedItems.Item(I)
    next
    
    0 讨论(0)
  • 2021-01-28 02:22

    When you delete (or move) item 1, item 2 moves into position 1. You skip that item and move on to item 3 which is now in position 2. For Each works the same way.

    One way of dealing with this is For i = oInboxFolder.Items.Count to 1 step -1

    0 讨论(0)
提交回复
热议问题