in reply to Re: Outlook OLE Delete Attachments
in thread Outlook OLE Delete Attachments

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.

Replies are listed 'Best First'.
Re^3: Outlook OLE Delete Attachments
by Lotus1 (Vicar) on Oct 17, 2014 at 14:03 UTC

    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.