I'm trying to automatize the deletion of secondary mailboxes items that are older than 6 days. The problem is that the objFolder and the SI_Items variables didn't initialize correctly in the script below. This script works perfectly if I change it a little bit in order to launch it in my own mailbox.
Can you please help me to find a solution ?
Sub RemoveOldEmails()
Dim objOL As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim SI_Items As Outlook.MailItem
Dim objFolder As Outlook.Folder
Dim myRecipient As Outlook.Recipient
On Error Resume Next
Set objOL = CreateObject("outlook.application")
Set objNS = objOL.GetNamespace("MAPI")
Set myRecipient = objNS.CreateRecipient("TEST-AUD")
myRecipient.Resolve
Set objFolder = objNS.GetSharedDefaultFolder(myRecipient, olFolderInbox)
Set SI_Items = objFolder.Items
For i = SI_Items.Count To 1 Step -1
If TypeName(SI_Items.Item(i)) = "MailItem" Then
If Date - SI_Items.Item(i).ReceivedTime > 6 Then SI_Items.Item(i).Delete
End If
Next
Set olApp = Nothing
Set objNS = Nothing
Set objFolder = Nothing
Set SI_Items = Nothing
Set myRecipient = Nothing
End Sub