in reply to Outlook OLE Delete Attachments

The approach I would take to debug this is to sprinkle print statements to log to csv in all those nested for and if blocks. Print maybe the subject, date, and attachment filename. Maybe simplify your code to just list all the attachments and start with a small group of emails, some with and some without attachments. Compare your logged output.

Also I would get rid of the C style for loops and do Perl style foreach loops over lists of attachments. In a quick search I found use Win32::OLE qw(in with); to import in() to let you get a list of objects. Something like foreach my $msg (in $Folder->{Items}){.

Here is an example of a similar question. Here is an even better example here at Perlmonks: Re: Win32::OLE Examples?.

Replies are listed 'Best First'.
Re^2: Outlook OLE Delete Attachments
by cormanaz (Deacon) on Oct 17, 2014 at 12:18 UTC
    It's looping through the messages and attachments fine, and I guess I could use a different statement to loop. But the problem is that it's not actually deleting the attachments like it's supposed to. On some further investigation I found that I was not saving the message after I modified it to get rid of the attachment, so I added $msg->Save after the delete operation, but still the same result.

      This morning I realized that you are deleting elements of an array that you are looping over. So the foreach loop approach I suggested is not a good idea.

      Then I found this node from ten years ago where you were deleting attachments in Outlook using a method called Remove(). Did that not work for this?

      Your code:

      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"; }

      One thing I notice here is that you are setting $count before the for loop and then assuming it should decrement each time you call delete. However you are decrementing $count each time whether or not you delete! This is the problem and some print statements would have clearly shown this.