PERL ---> Move email from Inbox folder to another or vice versa in OUTLOOK

B

bony_dev

Hello Gurus,

I am trying to write a perl script that will move an email message from
one folder to another folder in OUTLOOK using WIN32:OLE. Is there any
meathod to do so? If anybody has done it, please let me know...Thanks a
lot..


-Bony
 
B

Brian Helterline

Hello Gurus,

I am trying to write a perl script that will move an email message from
one folder to another folder in OUTLOOK using WIN32:OLE. Is there any
meathod to do so? If anybody has done it, please let me know...Thanks a
lot..


-Bony

This should point you in the right direction

#!perl -w

use strict;
use warnings;


use Win32::OLE;
use Win32::OLE::Const 'Microsoft Outlook';


# option variables
my $verbose = 0; # set to 1 for debuggin
my $all_items = 0; # set to 1 to process all mail, else only process unread
my $filter = '[UnRead] = True';

my $outlook;

# use existing instance if Outlook is already running

eval {$outlook = Win32::OLE->GetActiveObject('Outlook.Application')};
die "Outlook not installed\n" if $@;

unless (defined $outlook) {
$outlook = Win32::OLE->new('Outlook.Application', 'Quit')
or die "Oops, cannot start Outlook $^E\n";
}

my $ol = Win32::OLE::Const->Load($outlook);

my $item_idx = 1;
my $namespace = $outlook->GetNamespace("MAPI");
my $inbox = $namespace->GetDefaultFolder(olFolderInbox);


my $mail_count = $inbox->UnReadItemCount;
$mail_count = $inbox->Items->Count if ( $all_items );
print STDERR "Item Count = $mail_count\n" if ($verbose);

my $mail_items = $inbox->Items;
my $item = $all_items ? $mail_items->Item(1) : $mail_items->Find($filter);

while ($item) {
if ($item->Class == olMail) { # skip non email (meeting requests, etc)
if ( $item->{'Subject'} eq 'subject') {
# do something with message
}
}
if ( $all_items ) {
if (++$item_idx > $mail_count) {
$item = undef;
}
else {
$item = $mail_items->Item($item_idx);
}
}
else {
$item = $mail_items->FindNext;
}
}

undef $outlook;
exit 0;
 
B

Ben Morrow

Quoth Brian Helterline said:
-=-=-=-=-=-

MIME is not welcome on Usenet. Please don't do that again.
This should point you in the right direction

#!perl -w

-w is redundant with warnings.
use strict;
use warnings;

use Win32::OLE;
use Win32::OLE::Const 'Microsoft Outlook';

# option variables
my $verbose = 0; # set to 1 for debuggin
my $all_items = 0; # set to 1 to process all mail, else only process unread
my $filter = '[UnRead] = True';

my $outlook;

# use existing instance if Outlook is already running

eval {$outlook = Win32::OLE->GetActiveObject('Outlook.Application')};
die "Outlook not installed\n" if $@;

unless (defined $outlook) {
$outlook = Win32::OLE->new('Outlook.Application', 'Quit')
or die "Oops, cannot start Outlook $^E\n";
}

IMHO this would be clearer as

my $outlook = eval {
Win32::OLE->GetActiveObject('Outlook.Application')
or Win32::OLE->new('Outlook.Application', 'Quit')
or die "Cannot start Outlook: $^E";
};
$@ and die $@;
my $ol = Win32::OLE::Const->Load($outlook);

You don't use this.
my $item_idx = 1;

This needn't be declared this early.
my $namespace = $outlook->GetNamespace("MAPI");
my $inbox = $namespace->GetDefaultFolder(olFolderInbox);


my $mail_count = $inbox->UnReadItemCount;
$mail_count = $inbox->Items->Count if ( $all_items );

You end up calling $inbox->URIC even if you didn't need to.

my $mail_count = $all_items
? $inbox->UnReadItemCount
: $inbox->Items->Count;

You don't really need to do this at all if !$all_items.
print STDERR "Item Count = $mail_count\n" if ($verbose);

my $mail_items = $inbox->Items;
my $item = $all_items ? $mail_items->Item(1) : $mail_items->Find($filter);

while ($item) {
if ($item->Class == olMail) { # skip non email (meeting requests, etc)

It would be cleaner to use loop control here, if you'd used a proper
continue block:

$item->Class == olMail or next;
if ( $item->{'Subject'} eq 'subject') {

No need to quote 'Subject'.
# do something with message
}
}
if ( $all_items ) {

As this is the 'next' part of a C-style for loop, it should go in a
continue block

while ($item) {
...
}
continue {
if ($all_items) {
...
}
...
}
if (++$item_idx > $mail_count) {
$item = undef;
}

This seems icky to me. Will the ->Items object really not return a
proper iterator, or an array?
else {
$item = $mail_items->Item($item_idx);
}
}
else {
$item = $mail_items->FindNext;
}
}

undef $outlook;

Does this do anything?

This is useless.

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top