Rob van der Woude's Scripting Pages

VBScript Scripting Techniques > OLE Automation > Outlook

Automate Outlook

Clean up the Sent folder: delete messages older than a set number of days in the Sent folder


Clean up Outlook's Sent folder
VBScript Code:
' Delete messages over 14 days old from Outlook's "Sent Items" folder
' Note: You may want to insert a 60 seconds delay and then add shortcuts
'       to this script and to Outlook in your Startup folder
' Script by Rob van der Woude

Option Explicit

Dim intMax, intOld
Dim objFolder, objItem, objNamespace, objOutlook

Const SENT =  5 ' Sent Items folder

intMax = 14 ' Messages older than this will be deleted (#days)
intOld =  0 ' Counter for the number of deleted messages

Set objOutlook = CreateObject( "Outlook.Application" )
Set objNamespace = objOutlook.GetNamespace( "MAPI" )

' Open default account (will fail if Outlook is closed)
' and delete Sent messages over 2 weeks old
objNamespace.Logon "Default Outlook Profile", , False, False    
Set objFolder = objNamespace.GetDefaultFolder( SENT )
For Each objItem In objFolder.Items
    ' Check the age of the message against the maximum allowed age
    If DateDiff( "d", objItem.CreationTime, Now ) > intMax Then
        intOld = intOld + 1
    End If
WScript.Echo "Deleted " & intOld & " messages over " & intMax & " days old"

Set objFolder    = Nothing
Set objNamespace = Nothing
Set objOutlook   = Nothing
Windows version: any
Network: any
Client software: Outlook
Script Engine: any
Summarized: Works in any Windows version, as long as Outlook (not Outlook Express) is installed.
[Back to the top of this page]

page last modified: 2018-04-16