Jump to content
Sign in to follow this  

Save and/or Delete all Outlook Attachments

Recommended Posts

This is posted in reply to a question found here:


There are many reasons for wanting to do this, for instance, moving your attachments out of your PST and zipping them to save space, or to reduce the amount of data you have up on the Exchange server.

This is not tested extensively, so make sure you back up your data before trying this. You will have to uncomment the actions you want to perform, and if you want to save, yo might want to set a different directory.

$oIEErrorHandler = ObjEvent("AutoIt.Error","ComErrFunc")
global $COMerrnotify = true
Func ComErrFunc()
    If IsObj($oIEErrorHandler) Then
        if $COMerrnotify then
            Status("--> ComErrFunc: COM Error Encountered in " & @ScriptName)
            Status("----> Scriptline = " & $oIEErrorHandler.scriptline)
            Status("----> Number Hex = " & Hex($oIEErrorHandler.number, 8))
            Status("----> Number = " & $oIEErrorHandler.number)
            Status("----> Win Description = " & StringStripWS($oIEErrorHandler.WinDescription, 2))
            Status("----> Description = " & StringStripWS($oIEErrorHandler.description, 2))
            Status("----> Source = " & $oIEErrorHandler.Source)
            Status("----> Help File = " & $oIEErrorHandler.HelpFile)
            Status("----> Help Context = " & $oIEErrorHandler.HelpContext)
            Status("----> Last Dll Error = " & $oIEErrorHandler.LastDllError)
        $HexNumber = Hex($oIEErrorHandler.number, 8)
    Return 0

$ol = objCreate("Outlook.Application")
$ns = $ol.session
$pst = $ns.PickFolder
if not isobj($pst) then exit


func cleanAttachments($folderObj)
    $Items = $folderObj.Items
    if $Items.count > 0 then 
        Status("Processing messages...")
        for $position = 1 to $Items.count
            $Item = $Items.Item($position)
            $itemAttachments = $Item.Attachments
            if $itemAttachments.count > 0 Then
                Status($folderObj.name &"\"& $Item.subject & ": " & $itemAttachments.count)
                for $a = $itemAttachments.count to 1 step -1 ; we must delete them in reverse order, obviously
                    $attachment = $itemAttachments.item($a)
                    ;uncomment next line to save each attachment
                    ;$attachment.SaveasFile(@ScriptDir & "\" & StringMid(TimerInit(),3,13) &"_"& $attachment.DisplayName)
                    ;uncomment next line to delete attachment
    $subfolders = $folderObj.Folders
    $subFolder = $subfolders.GetFirst
    while isobj($subFolder)
        $subFolder = $subfolders.GetNext

func Status($text)
    ConsoleWrite($text & @crlf)

Share this post

Link to post
Share on other sites

this is SWEET!!!

how would I just automatically select a folder (eg Inbox)

I'm using

$oOutlookExp = $oOutlookApp.ActiveExplorer
$pst = $oOutlookExp.CurrentFolder

But that means the inbox has to be selected. I don't know how to specify the inbox or another folder.

Edited by JohnBailey

A decision is a powerful thing

Share this post

Link to post
Share on other sites

Global Const $olFolderInBox = 6

;$pst = $ns.PickFolder

$pst = $ns.GetDefaultFolder($olFolderInBox)

GLORIOUS DAY!! That's how you use olFolder and such!!! I couldn't figure it out. I've tried so many different things, while reading through MSDN and using the example scripts.

So how did you figure out that it is 6 ?


A decision is a powerful thing

Share this post

Link to post
Share on other sites

I found a Delphi reference and translated it. I also posted that translation here:


DaleHohm pointed out to me in that post that there is a much easier way to enumerate the constants for COM objects, but this should get you started. Enjoy!

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Create New...