I've been building a small automated tool to go into a particular folder in Outlook, pull out attachments (which are reports I've set up to be sent to my email address in tab-delimited format), save them to a network directory for processing, and move the emails to a "processed" folder.
My first model worked okay, but I didn't particularly like the coding. It retrieved the number of the items in the folder, set an index variable, and then retrieved each item by the index. So I thought to myself,
self, it would be much nicer to pick up all the items into an array, and then cycle through that.
My attempt at this did not work. This didn't really surprise me, but I'm not sure how to get it to work. Eventually I'm sure I'd want to iterate through attachments, and it would feel clunky to keep getting counts in order to set up loops.
update: Hmmm, yes, the question: Anyone know how to make this "cleaner"?
#! perl -w
use strict;
use Win32::OLE;
my ($in, $to)=@ARGV;
my $mail = new Win32::OLE('Outlook.Application');
my $ns = $mail->GetNamespace("MAPI");
my $inbox = $ns->GetDefaultFolder(6); ## the Inbox
my $infolder = $inbox->Folders($in);
my $tofolder = $inbox->Folders($to);
my $count = $infolder->Items->Count;
print "There are $count messages in the $in folder\n";
my $i=0;
while($i <= $count){
#the original, which worked, but I didn't particularly care fo
+r
#the way the iteration was carried out
#my $itm = $infolder->Items($i);
## the below line works (it returns a reference to something,
+but...
my $itms = $infolder->Items;
for (@$itms){ ## for some reason $itms is not an array reference
## so. . . what is it?
my $bdy = $_->Body;
#the original line, which worked
#my $bdy = $itm->Body;
print "$bdy\n";
my $atm = $_->Attachments(1);
#original line below
#my $atm = $itm->Attachments(1);
my $atmname = $atm->FileName;
$_->Attachments(1)->SaveAsFile("H:\\erepts\\$i.$atmname");
$_->Move($tofolder);
#below worked fine, saved to a folder on the network
#$itm->Attachments(1)->SaveAsFile("H:\\erepts\\$i.$atmname");
#$itm->Move($tofolder);
}
$i++;
}
$mail->Quit();
PS: What the heck's with the 1-based collections in VBA, anyway? Must've taken me ten minutes to figure out how a zero subscript would be "out of range" ;)
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.