Skip to content

Instantly share code, notes, and snippets.

@NeraSnow
Created January 8, 2023 00:03
Show Gist options
  • Save NeraSnow/62245875483c8b5e5e27ca40ccc039f3 to your computer and use it in GitHub Desktop.
Save NeraSnow/62245875483c8b5e5e27ca40ccc039f3 to your computer and use it in GitHub Desktop.
Outlook Archive Old Emails VBA Script
Sub MoveAgedMail()
Dim objOutlook As Outlook.Application
Dim objNamespace As Outlook.NameSpace
Dim objSourceFolder As Outlook.MAPIFolder
Dim objDestFolder As Outlook.MAPIFolder
Dim objVariant As Variant
Dim lngMovedItems As Long
Dim intCount As Integer
Dim intDateDiff As Integer
Dim strDestFolder As String
Set objOutlook = Application
Set objNamespace = objOutlook.GetNamespace("MAPI")
Set objSourceFolder = objNamespace.GetDefaultFolder(olFolderInbox)
' Destination
Set objDestFolder = objNamespace.Folders("YOUR_EMAIL").Folders("Archive")
For intCount = objSourceFolder.Items.Count To 1 Step -1
Set objVariant = objSourceFolder.Items.Item(intCount)
DoEvents
If objVariant.Class = olMail Then
intDateDiff = DateDiff("d", objVariant.SentOn, Now)
' I'm using 31 days, adjust as needed.
If intDateDiff > 31 Then
objVariant.Move objDestFolder
'count the # of items moved
lngMovedItems = lngMovedItems + 1
End If
End If
Next
' Display the number of items that were moved.
MsgBox "Moved " & lngMovedItems & " messages(s)."
Set objDestFolder = Nothing
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment