Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 42225

MsOf10 OUTLOOK - script to keep only the most recent email in a folder

$
0
0
Hi All,

Everyday I receive an email with an access code in it, which is only valid for the current day.
I have an outlook rule set up to put it in to it's own folder (on the same folder level as my Inbox).

What I would like to do is add a script to the rule, to delete all the older emails when the new one comes in.
I have found the code below on another forum somewhere (can't recall) which seems to work, except that you have to pick which folder you are clearing out each time.
I would like to specify the folder, but as yet I have been unsuccessful.
What should I replace the PickFolder line with?

Code:

Sub DeleteOlderThan1day()
 
Dim oFolder As Folder
Dim Date1day As Date
Dim ItemsOverMonths As Outlook.Items
Dim DateToCheck As String
 
Date1day = DateAdd("d", -1, Now())
Date1day = Format(Date1day, "mm/dd/yyyy")
 
Set oFolder = Application.Session.PickFolder 'or set your folder
 
DateToCheck = "[Received] <= """ & Date1day & """"
 
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

Could anyone help me with this?
(Outlook 2010, POP3 email account.)
Thanks!

Viewing all articles
Browse latest Browse all 42225

Trending Articles