VBA to delete Outlook messages from a user created folder over certain time period

后端 未结 1 1432
[愿得一人]
[愿得一人] 2021-01-01 08:11

I\'m looking for a VBA code snippet to delete Outlook messages that are six (6) months or older and sit in a user-created folder.

What does that code look like?

1条回答
  •  借酒劲吻你
    2021-01-01 08:57

    Here is a example script

    Sub DeleteOlderThan6months()
    
    Dim oFolder As Folder
    Dim Date6months As Date
    Dim ItemsOverMonths As Outlook.Items
    
    Dim DateToCheck As String
    
    Date6months = DateAdd("d", -182, Now())
    Date6months = Format(Date6months, "mm/dd/yyyy")
    
    
    Set oFolder = Application.Session.PickFolder 'or set your folder
    
    DateToCheck = "[Received] <= """ & Date6months & """"
    
    Set ItemsOverMonths = oFolder.Items.Restrict(DateToCheck)
    
    For i = ItemsOverMonths.Count To 1 Step -1
        ItemsOverMonths.Item(i).Delete
    Next
    
    
    Set ItemsOverMonths = Nothing
    Set oFolder = Nothing
    
    
    End Sub
    

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