Sign in to follow this  
Followers 0
lod3n

Save and/or Delete all Outlook Attachments

8 posts in this topic

This is posted in reply to a question found here:

http://www.autoitscript.com/forum/index.php?showtopic=31022

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)
        EndIf
        $HexNumber = Hex($oIEErrorHandler.number, 8)
        SetError($HexNumber)
    Else
        SetError(1)
    EndIf
    Return 0
EndFunc

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

cleanAttachments($pst)

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
                    ;$attachment.Delete()
                    
                Next
            EndIf
        Next
    EndIf
    
    $subfolders = $folderObj.Folders
    $subFolder = $subfolders.GetFirst
    while isobj($subFolder)
        cleanAttachments($subFolder)
        $subFolder = $subfolders.GetNext
    WEnd
EndFunc

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

[font="Fixedsys"][list][*]All of my AutoIt Example Scripts[*]http://saneasylum.com[/list][/font]

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

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 ?

btw THANK YOU!!


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:

http://www.autoitscript.com/forum/index.php?showtopic=35965

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!


[font="Fixedsys"][list][*]All of my AutoIt Example Scripts[*]http://saneasylum.com[/list][/font]

Share this post


Link to post
Share on other sites

Brilliant! Thank you!! This is so exciting!! I learned a lot while beating my mind over how to do it. Thanks again


A decision is a powerful thing

Share this post


Link to post
Share on other sites

Dude, lod3n, Dale's stuff is amazing! I'm so glad you guys are sharing this stuff so we can learn it! Seriously cool!


A decision is a powerful thing

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  
Followers 0