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;