<?xml version="1.0" encoding="windows-1252"?>
<node id="133774" title="Extracting memos from an Outlook Inbox" created="2001-12-21 14:39:03" updated="2005-08-14 08:21:17">
<type id="1748">
sourcecode</type>
<author id="29008">
grinder</author>
<data>
<field name="doctext">
&lt;code&gt;
#! /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 =&gt; 6;

my $r;
eval {
  $r = Win32::OLE-&gt;GetActiveObject('Outlook.Application')
};
if ($@ || !defined($r)) {
  $r = Win32::OLE-&gt;new('Outlook.Application', sub {$_[0]-&gt;Quit;}) or die "oops\n";
}

my $namespace = $r-&gt;GetNameSpace( 'MAPI' )     or die "can't open MAPI namespace\n";

my $count = 0;
my $folder;

if( $folder = $namespace-&gt;GetDefaultFolder( INBOX )) {
  print "$folder-&gt;{Name}\n";
  my $f = $folder;
  foreach( @ARGV ) {
    # re-reading this, I remember what this is for
    # you pass the names of the sub sub &amp;#091;...] 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-&gt;Folders( $_ ); # walk down the folder path
    print "$f-&gt;{Name}\n" or die Win32::OLE-&gt;LastError() . "\n";
  }
  foreach my $it (reverse in $f-&gt;Items) {
    ++$count;
    print "$it-&gt;{ReceivedTime} $it-&gt;{Subject}\n";
    my $subj = $it-&gt;{Subject};
    $subj =~ tr/a-zA-Z0-9/_/c;
    $subj =~ s/_+/_/g;
    open OUT, sprintf( "&gt;%03s-$subj.txt", $count ) or die "Cannot open $count for output: $!\n";
    print OUT &lt;&lt;EOM;
    
To: $it-&gt;{To}
CC: $it-&gt;{CC}
From: $it-&gt;{SenderName}
Subject: $it-&gt;{Subject}
Date: $it-&gt;{ReceivedTime}

$it-&gt;{Body}
EOM
    close OUT;
  }
}
&lt;/code&gt;
</field>
<field name="codedescription">
Someone in the chatter box asked whether anybody had any Perl code to deal with Outlook on Windows.&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;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 &lt;tt&gt;in&lt;/tt&gt; 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.&lt;/p&gt;

&lt;p&gt;Oh well, it's a starting point.&lt;/p&gt;</field>
<field name="codecategory">
Win32 Stuff</field>
<field name="codeauthor">
[grinder]</field>
</data>
</node>
