patgas has asked for the wisdom of the Perl Monks concerning the following question:

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!

Replies are listed 'Best First'.
Re: Foreach-ing an OLE Collection object
by cacharbe (Curate) on Aug 09, 2001 at 16:42 UTC
    The following worked fine for me, so I need to ask, what problems are you having?

    use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Outlook'; $Win32::OLE::Warn = 2; # Throw Errors, I'll catch them my $OL = Win32::OLE->GetActiveObject('Outlook.Application') || Win32::OLE->new('Outlook.Application', 'Quit'); my $NameSpace = $OL->GetNameSpace("MAPI"); my $Folder = $NameSpace->GetDefaultFolder(olFolderInbox); foreach my $msg (in $Folder->{Items}){ foreach my $atch (in $msg->{Attachments}){ my $filename = "C:\\atchs\\" . $atch->{FileName}; $atch->SaveAsFile($filename); } }
    C-.
Re: Foreach-ing an OLE Collection object
by John M. Dlugosz (Monsignor) on Aug 09, 2001 at 07:28 UTC
    I had a problem with 'in' reciently too. Try putting the "argument" in curly braces if it's not a simple variable. E.g. in {$this->that}
Re: Foreach-ing an OLE Collection object
by patgas (Friar) on Aug 09, 2001 at 21:31 UTC

    Okay, I used John M. Dlugosz's suggestion, and I wrapped the entire collection call in curlies, like so:

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

    This works perfectly for me... When I tried cacharbe's suggestion of doing it this way:

    in $folder->{Items}
    , I got this error:

    Win32::OLE(0.15) error 0x80020003: "Member not found" at bestshots.pl +line 47 Win32::OLE(0.15): GetOleEnumObject() Not a Win32::OLE::Enum object at +C:/Perl/si te/lib/Win32/OLE/Lite.pm line 167. Can't use string ("0") as a HASH ref while "strict refs" in use at bes +tshots.pl line 47.
    -- More than perfect! Let us engage the Concord!
      hmmm. Well, I don't know what to say...I ran my code here on:
      • Win2k Advanced Server
      • ActivePerl 5.6.1 build 628
      and it ran like a champ.

      C-.