I've got the following code that loops through my Outlook inbox and saves out the attachments (shortened for brevity's sake:

my $namespace = $outlook->GetNameSpace("MAPI"); my $folder = $namespace->GetDefaultFolder(olFolderInbox); for ( my $i = 1; $i <= $folder->Items->Count; $i++ ) { for ( my $j = 1; $j <= $folder->Items($i)->Attachments->Count; $j+ ++ ) { print "Saving " . $folder->Items($i)->Attachments($j)->FileNam +e . " from \"" . $folder->Items($i)->Subject . "\"\n"; $folder->Items($i)->Attachments($j)->SaveAsFile( "C:\\Download +\\" . $folder->Items($i)->Attachments($j)->FileName ); } }

Now, this works and all, but it seems like it could be cleaned up using foreach loops instead of those nasty index variables. I attempted various things like this:

foreach my $item ( $folder->Items ) { foreach my $attachment ( $item->Attachments ) { print "Saving " . $attachment->FileName . " from \"" . $item-> +Subject . "\"\n"; $attachment->SaveAsFile( "C:\\Download\\" . $attachment->FileN +ame ) ; } }

I tried saving out the collections to other variables, to lists, I tried using curly brackets around the various properties, I even tried using this mysterious "in" operator that showed up in Win32::OLE, and that didn't work either. Anyone have suggestions?

-- More than perfect! Let us engage the Concord!

In reply to Foreach-ing an OLE Collection object by patgas

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.