# outlook delete mail older than x days



## ADAMC (Jun 20, 2011)

Hi all 

I found a macro that is perfect for what i need (almost)
It allows me to choose a mailbox then deletes e-mails in the mailbox older than x days (specified in the code)

Only 2 people require this macro my machine runs outlook 2007 macro works
colleagues machine runs outlook 2003 and it does not work and errors out at the line:
Dim oFolder As Folder

Anyone any ideas why or can help me with a macro to delete emals from any mailbox folder x days or older?

Sub DeleteOlderThanxmonths()
Dim oFolder As Folder
Dim Date6months As Date
Dim ItemsOverMonths As Outlook.Items
Dim DateToCheck As String
Date6months = DateAdd("d", -1, 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

P.S I cant use auto archive feature as it seems to be disabled by default within my company.

Thank you!


----------



## Derek Brown (Jun 20, 2011)

I cannot check this idea as I am using 2007, but have you tried changing it to:
"Dim oFolder As Outlook.Folder"
There were a number of changes in Outlook 2007 regarding the way that folders were referenced and accessed.


----------

