in reply to Re: How do I convert VBA script to Perl? - Using Win32::OLE
in thread How do I convert VBA script to Perl? - Using Win32::OLE

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re^3: How do I convert VBA script to Perl? - Using Win32::OLE
by ikegami (Patriarch) on Jan 24, 2006 at 18:55 UTC
    • 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.

    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.
Re^3: How do I convert VBA script to Perl? - Using Win32::OLE
by jdporter (Paladin) on Jan 24, 2006 at 18:00 UTC

    I believe you need to say

    $ns = $Outlook->GetNamespace('MAPI');
    We're building the house of the future together.