nt1
12-18-2015, 03:31 PM
Hi guys,
I'm using a custom VBA script that deletes emails older than 90 days unless categorized as "Archive".
I noticed it only deletes in the default folders like inbox, sent etc. but never gets to delete in the subfolders.
I am using Outlook 2013.
Why does this script not also look in subfolders?
Private Sub Application_Startup()
'when outlook opens
RemoveEmail90
End Sub
Public Sub RemoveEmail90()
'Permanently delete items older than 90 days that do NOT have the category 'Archive' attached.
'Applies to Inbox, Draft, Sent and Junk
'Runs on startup
Dim olInbox As Outlook.Folder
Dim olDraft As Outlook.Folder
Dim olSent As Outlook.Folder
Dim olJunk As Outlook.Folder
Dim olDeleted As Outlook.Folder
Dim i As Integer
Set olInbox = Application.Session.GetDefaultFolder(olFolderInbox)
Set olDraft = Application.Session.GetDefaultFolder(olFolderDrafts)
Set olSent = Application.Session.GetDefaultFolder(olFolderSentMail)
Set olJunk = Application.Session.GetDefaultFolder(olFolderJunk)
Set olDeleted = Application.Session.GetDefaultFolder(olFolderDeletedItems)
'''''''''' INBOX
Set Outlook_Items = olInbox.Items
For i = (Outlook_Items.Count) To 1 Step -1
If DateDiff("d", Outlook_Items.Item(i).CreationTime, Now) > 90 Then
If (InStr(Outlook_Items.Item(i).Categories, "Archive") = 0) Then
Outlook_Items.Item(i).Delete
End If
End If
Next
''''''''''' DRAFT
Set Outlook_Items = olDraft.Items
For i = (Outlook_Items.Count) To 1 Step -1
If DateDiff("d", Outlook_Items.Item(i).CreationTime, Now) > 90 Then
If (InStr(Outlook_Items.Item(i).Categories, "Archive") = 0) Then
Outlook_Items.Item(i).Delete
End If
End If
Next
''''''''''' SENT
Set Outlook_Items = olSent.Items
For i = (Outlook_Items.Count) To 1 Step -1
If DateDiff("d", Outlook_Items.Item(i).CreationTime, Now) > 90 Then
If (InStr(Outlook_Items.Item(i).Categories, "Archive") = 0) Then
Outlook_Items.Item(i).Delete
End If
End If
Next
''''''''''' JUNK
Set Outlook_Items = olJunk.Items
For i = (Outlook_Items.Count) To 1 Step -1
If DateDiff("d", Outlook_Items.Item(i).CreationTime, Now) > 90 Then
If (InStr(Outlook_Items.Item(i).Categories, "Archive") = 0) Then
Outlook_Items.Item(i).Delete
End If
End If
Next
''''''''''' DELETED
Set Outlook_Items = olDeleted.Items
For i = (Outlook_Items.Count) To 1 Step -1
If DateDiff("d", Outlook_Items.Item(i).CreationTime, Now) > 90 Then
If (InStr(Outlook_Items.Item(i).Categories, "Archive") = 0) Then
'======= this is the line (below) to uncomment after first run ===============
Outlook_Items.Item(i).Delete
'==============================================================
'If it's the first run, leave the above line commented - it will leave emails in the deleted items
'this is a safety measure
'Check if the script ran correctly, Empty deleted items manually,
'uncheck comment, save.
End If
End If
Next
Set olInbox = Nothing
Set olDraft = Nothing
Set olSent = Nothing
Set olJunk = Nothing
Set olDeleted = Nothing
End Sub
I'm using a custom VBA script that deletes emails older than 90 days unless categorized as "Archive".
I noticed it only deletes in the default folders like inbox, sent etc. but never gets to delete in the subfolders.
I am using Outlook 2013.
Why does this script not also look in subfolders?
Private Sub Application_Startup()
'when outlook opens
RemoveEmail90
End Sub
Public Sub RemoveEmail90()
'Permanently delete items older than 90 days that do NOT have the category 'Archive' attached.
'Applies to Inbox, Draft, Sent and Junk
'Runs on startup
Dim olInbox As Outlook.Folder
Dim olDraft As Outlook.Folder
Dim olSent As Outlook.Folder
Dim olJunk As Outlook.Folder
Dim olDeleted As Outlook.Folder
Dim i As Integer
Set olInbox = Application.Session.GetDefaultFolder(olFolderInbox)
Set olDraft = Application.Session.GetDefaultFolder(olFolderDrafts)
Set olSent = Application.Session.GetDefaultFolder(olFolderSentMail)
Set olJunk = Application.Session.GetDefaultFolder(olFolderJunk)
Set olDeleted = Application.Session.GetDefaultFolder(olFolderDeletedItems)
'''''''''' INBOX
Set Outlook_Items = olInbox.Items
For i = (Outlook_Items.Count) To 1 Step -1
If DateDiff("d", Outlook_Items.Item(i).CreationTime, Now) > 90 Then
If (InStr(Outlook_Items.Item(i).Categories, "Archive") = 0) Then
Outlook_Items.Item(i).Delete
End If
End If
Next
''''''''''' DRAFT
Set Outlook_Items = olDraft.Items
For i = (Outlook_Items.Count) To 1 Step -1
If DateDiff("d", Outlook_Items.Item(i).CreationTime, Now) > 90 Then
If (InStr(Outlook_Items.Item(i).Categories, "Archive") = 0) Then
Outlook_Items.Item(i).Delete
End If
End If
Next
''''''''''' SENT
Set Outlook_Items = olSent.Items
For i = (Outlook_Items.Count) To 1 Step -1
If DateDiff("d", Outlook_Items.Item(i).CreationTime, Now) > 90 Then
If (InStr(Outlook_Items.Item(i).Categories, "Archive") = 0) Then
Outlook_Items.Item(i).Delete
End If
End If
Next
''''''''''' JUNK
Set Outlook_Items = olJunk.Items
For i = (Outlook_Items.Count) To 1 Step -1
If DateDiff("d", Outlook_Items.Item(i).CreationTime, Now) > 90 Then
If (InStr(Outlook_Items.Item(i).Categories, "Archive") = 0) Then
Outlook_Items.Item(i).Delete
End If
End If
Next
''''''''''' DELETED
Set Outlook_Items = olDeleted.Items
For i = (Outlook_Items.Count) To 1 Step -1
If DateDiff("d", Outlook_Items.Item(i).CreationTime, Now) > 90 Then
If (InStr(Outlook_Items.Item(i).Categories, "Archive") = 0) Then
'======= this is the line (below) to uncomment after first run ===============
Outlook_Items.Item(i).Delete
'==============================================================
'If it's the first run, leave the above line commented - it will leave emails in the deleted items
'this is a safety measure
'Check if the script ran correctly, Empty deleted items manually,
'uncheck comment, save.
End If
End If
Next
Set olInbox = Nothing
Set olDraft = Nothing
Set olSent = Nothing
Set olJunk = Nothing
Set olDeleted = Nothing
End Sub