cormanaz has asked for the wisdom of the Perl Monks concerning the following question:
When I run this, it seems to delete the attachments in some cases but not all. When it does delete them, if I run it again on the same message it will show an attachment count, but there are no Items in the attachment object (why I had to add the defined clause in line 29).#!/usr/bin/perl -w use strict; use Win32::OLE; use Win32::OLE::Variant; use Win32::OLE::Const 'Microsoft Outlook'; use Date::Manip; # use existing instance if Outlook is already running, or launce a new + one 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 "Oops, cannot start Outlook"; } my $cutoffdate = ParseDate("May 1, 2014"); 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->Item($i); my $msgdate = getTimeStamp($msg->{ReceivedTime}); if (Date_Cmp($cutoffdate,$msgdate) == 1) { my $count = $msg->{Attachments}->Count; if ($count > 0) { for (my $i = $count; $i > 0; $i--) { if (defined($msg->{Attachments}->Items($i))) { $msg->{Attachments}->Items($i)->Delete; } } print UnixDate($msgdate,"%Y-%m-%d %i:%M:%S %p")," ",substr +($msg->{Subject},0,30)."... $count attachments deleted\n"; } } } 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")); }
I couldn't find any existing Perl code to do this job, so I am basing the code on this VB example. But obviously I'm not doing something right. Anyone know how to fix?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Outlook OLE Delete Attachments
by Lotus1 (Vicar) on Oct 17, 2014 at 01:43 UTC | |
by cormanaz (Deacon) on Oct 17, 2014 at 12:18 UTC | |
by Lotus1 (Vicar) on Oct 17, 2014 at 14:03 UTC | |
|
Re: Outlook OLE Delete Attachments
by Anonymous Monk on Oct 17, 2014 at 13:46 UTC | |
by Lotus1 (Vicar) on Oct 18, 2014 at 17:24 UTC |