Ha. Well I remembered that I had written such a script a long time ago but had lost the code. Turns out I posted here for help on it in 2004, and I found the node in a dusty corner of the Monastery basement. Problem was that you have to use a Remove method rather than a Delete method. The working code is below for anyone else who's interested (sans the C-style loop, which really seems to bother some of the Brothers).

Now the odd thing is that the purpose of running this was to free up mailbox space, but after running it and deleting a couple hundred attachments, some large, my mailbox size didn't change at all. Need to consult the Exchange admin on that one.

#!/usr/bin/perl -w use strict; use Win32::OLE; use Win32::OLE::Variant; use Win32::OLE::Const 'Microsoft Outlook'; use Date::Manip; my $ol; eval {$ol = Win32::OLE->GetActiveObject('Outlook.Application')}; die "Outlook not installed" if $@; unless (defined $ol) { $ol = Win32::OLE->new('Outlook.Application', sub {$_[0]->Quit;}) or die "Cannot start Outlook"; } my $cutoffdate = ParseDate("December 31, 2012"); my $mailbox = seekFolder($ol->Session, 'foo@bar.com'); my $folder = seekFolder($mailbox, 'Sent Items'); my $end = $folder->Items->Count; for my $i (1..$end) { my $msg = $folder->Items($i); my $msgdate = getTimeStamp($msg->{ReceivedTime}); if (Date_Cmp($cutoffdate,$msgdate) == 1) { my $atch = $msg->Attachments; my $deleted = 0; if ($atch->Count) { while ($atch->Count) { $atch->Remove(1); $deleted++; } $msg->Save; } if ($deleted) { print UnixDate($msgdate,"%Y-%m-%d %i:%M:%S %p")," ",substr +($msg->{Subject},0,30)."... $deleted attachments deleted\n"; } } else { last; } } Win32::OLE->FreeUnusedLibraries(); sub seekFolder { my $obj = shift; my $target = shift; for (my $i = 1; $i <= $obj->Folders->Count; $i++) { if ( $obj->Folders->Item($i)->Name eq $target ) { return $obj->Folders->Item($i); } } } sub getTimeStamp { my ($var) = @_; my $timestamp = Win32::OLE::Variant->new(VT_DATE,$var); return ParseDate($timestamp->Date("yyyyMMdd").$timestamp->Time("HH +mmss")); }

In reply to Re: Outlook OLE Delete Attachments by Anonymous Monk
in thread Outlook OLE Delete Attachments by cormanaz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.