in reply to Re^2: How do I convert VBA script to Perl? - Using Win32::OLE
in thread How do I convert VBA script to Perl? - Using Win32::OLE
my $ns = GetNamespace("MAPI");
should probably be
my $ns = $Outlook->GetNamespace("MAPI");
Yes, return; is indeed the equivalent of Exit Sub.
Your indentation is inconsistent, making your code hard to read.
$i serves no purpose. Same as in the VB code.
$ol is never used either.
$Atmt is never assigned a value. Change
my $Item; my $Atmt; ... foreach (in $Inbox->Items) { foreach (in $Atmt->Item->Attachments) {
to
foreach my $Item (in $Inbox->Items) { foreach my $Atmt (in $Item->Attachments) {
The following (and the VB equivalent) is useless since the variables get freed when they go out of scope:
undef $Atmt; undef $Item; undef $ns;
The return at the end of the function is useless.
So we get:
#! perl use strict; use warnings; use File::Spec (); use Win32::OLE qw( in ); use Win32::OLE::Const 'Microsoft Outlook'; my $SAVE_FOLDER = 'G:\Input'; my $Outlook = Win32::OLE->new('Outlook.Application', 'Quit'); Detach_files(); sub Detach_files { my $ns = $Outlook->GetNamespace('MAPI'); my $Inbox = $ns->GetDefaultFolder(olFolderInbox); return if $Inbox->Items->{Count} == 0; foreach my $Item (in $Inbox->Items) { # Check each message for attachments. foreach my $Atmt (in $Item->Attachments) { # Save any "Flat_file_" attachments found. my $FileName = $Atmt->FileName; if ($FileName =~ /^Flat_file_/) { $FileName = File::Spec->catdir($SAVE_FOLDER, $FileName); $Atmt->SaveAsFile($FileName); } } } }
Untested.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
| A reply falls below the community's threshold of quality. You may see it by logging in. | |
| A reply falls below the community's threshold of quality. You may see it by logging in. |