Category: | Win32 Stuff |
Author/Contact Info | grinder |
Description: | Someone in the chatter box asked whether anybody had any Perl code to deal with Outlook on Windows.
I used Outlook at my previous job, and when I left I wrote the following code to extract all the messages and dump them out into files. The code is rather exploratory and not really well-written. For a start, it consumes a horrendous amount of RAM, directly proportional to the size of your Inbox (because in instantiates all the objects in an array that unfortunately is not evaluated lazily). A lot of the magic numbers come from MSDN documentation, but the accompanying notes I wrote at the time appear to have drifted away. Oh well, it's a starting point. |
#! /usr/perl/bin -w # # david landgren 28-sep-1999 # copyright (c) David Landgren 2001 # This program is free software, it is distributed # under the sames terms as Perl itself. (Did I get that right?) use strict; use Win32::OLE qw/in/; use constant INBOX => 6; my $r; eval { $r = Win32::OLE->GetActiveObject('Outlook.Application') }; if ($@ || !defined($r)) { $r = Win32::OLE->new('Outlook.Application', sub {$_[0]->Quit;}) or d +ie "oops\n"; } my $namespace = $r->GetNameSpace( 'MAPI' ) or die "can't open MAPI + namespace\n"; my $count = 0; my $folder; if( $folder = $namespace->GetDefaultFolder( INBOX )) { print "$folder->{Name}\n"; my $f = $folder; foreach( @ARGV ) { # re-reading this, I remember what this is for # you pass the names of the sub sub [...] folder # you want to examine as arguments on the command line # e.g. perl extract.pl archive 1999 lists vmsperl mkdir $_, 0666; chdir $_; $f = $f->Folders( $_ ); # walk down the folder path print "$f->{Name}\n" or die Win32::OLE->LastError() . "\n"; } foreach my $it (reverse in $f->Items) { ++$count; print "$it->{ReceivedTime} $it->{Subject}\n"; my $subj = $it->{Subject}; $subj =~ tr/a-zA-Z0-9/_/c; $subj =~ s/_+/_/g; open OUT, sprintf( ">%03s-$subj.txt", $count ) or die "Cannot open + $count for output: $!\n"; print OUT <<EOM; To: $it->{To} CC: $it->{CC} From: $it->{SenderName} Subject: $it->{Subject} Date: $it->{ReceivedTime} $it->{Body} EOM close OUT; } } |
Back to
Code Catacombs