Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Extracting memos from an Outlook Inbox

by grinder (Bishop)
on Dec 21, 2001 at 19:39 UTC ( [id://133774] : sourcecode . print w/replies, xml ) Need Help??
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 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) {
    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}

    close OUT;
Replies are listed 'Best First'.
Re: Extracting memos from an Outlook Inbox
by dmmiller2k (Chaplain) on Dec 21, 2001 at 21:13 UTC

    Wow! Thanks. Exactly what I need.


    You can give a man a fish and feed him for a day ...
    Or, you can
    teach him to fish and feed him for a lifetime
Re: Extracting memos from an Outlook Inbox
by Anonymous Monk on Oct 27, 2002 at 12:19 UTC
    Hey!! It's cool!!! For my system (Windows 2kp<=>Outlook 2000, perl v5.6.1.), this script is working properly (else to see something like this: "Win32::OLE::Variant=SCALAR(0x21d2f88)" instead of date and time) only with following subroutine:
    print "$it->{ReceivedTime} $it->{Subject}\n";
    #-------------------my sub----------------------- sub time{ my $var = Variant(VT_DATE,$it->{ReceivedTime} ); my $OleObject->{value} = $var; my $OleObject->Method($var); } #------------------------------------------------
    my $subj = $it->{Subject};
    Of course, together with: "use Win32::OLE::Variant;". else, it's perfect! : )) Stefan Marchitan