Wed 26 Mar 2008
I wanted to make a program to scan a particular folder in my outlook 2003 that holds messages placed there by a rule. However, due to outlook security, a pop up appears to confirm access to outlook when you try to access the emails. This is a major roadblock to automating email access on windows xp. I could not find a clear example on the web that used another pst file so I created this example. I hope this helps someone out down the line.
There is a solution called Outlook Redemption that solved the pop-up problem for me. You can install a copy on windows from the site http://www.dimastr.com/redemption/ .
Once you have this installed, make sure you have a copy of strawberry perl http://strawberryperl.com/ . I happen to use version 5.8 but I will switch to 5.10 soon. With this installed, you will also need the module Win32 installed, but this is easy since you can install perl modules with CPAN on windows when you have strawberry perl setup.
Here is the code that scans a different pst file called otherpst and a folder within that pst file called scanme
#!perl # windows notation
use strict;
use Win32::OLE;
use Win32::OLE::Const;
use Win32::OLE::Const ‘Microsoft Outlook’;
my $outlook;
eval {
$outlook = Win32::OLE->GetActiveObject(’Outlook.Application’)
};
if ($@ || !defined($outlook)) {
$outlook = Win32::OLE->new(’Outlook.Application’, sub {$_[0]->Quit;})
or die(”Cannot create outlook\n”);
}
my $namespace = $outlook->GetNamespace(’MAPI’);
# get the proper pst file
my $folder = $namespace->{’Folders’}{’otherpst’};
# now get the folder within the pst file
my $folder2 = $folder->{’Folders’}{”scanme”};
# get all the messages
my $msgs = $folder2->{Items};
# a count of the number of messages in the folder
my $ncon = $msgs->{Count};
# a redemption object that stops the annoying popup
my $tysonmaly = new Win32::OLE(’Redemption.SafeMailItem’);
my $con;
# for each message in the scanme folder print the body out to STDOUT
foreach my $ii (1 .. $ncon) {
$con = $msgs->Item($ii);
$tysonmaly->{’Item’} = $con;
# you can access other things like the subject or sent time or sender but this just shows body access
print $tysonmaly->{Body} . “\n”;
}