This script runs through an Outlook Express mail folder and pulls out email information for all mail after a given date. The information is then inserted into a table in a MS Word document to build a correspondence list as part of a meeting agenda document.

Much of the code dealing with the location of things is hard wired and will need to be changed in (I hope) obvious ways for your own application.

use strict; use warnings; use Mail::Transport::Dbx; use Win32::OLE; # Object Linking and Embedd +ing use Win32::OLE::Const ('Microsoft Word'); # Defines constants word +knows my $date = '2009-11-10'; my $lastEmail = '2009-10-08'; my $path = 'C:\Documents and Settings\User\Local Settings\Application +Data\Identities\{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}\Microsoft\Outl +ook Express'; my $docDir = 'C:\Documents and Settings\User\My Documents\MeetingStuff +\'; my $docName = "$docDir\\LOC ${date} Agenda_new.doc"; my %emailLists = ( 'Bill; Ben' => 'pots', 'fred@erehwon.com' => 'lost', ); my $dbx = eval {Mail::Transport::Dbx->new ("$path/Interesting Email.db +x")}; die $@ if $@; my @emails; for my $i (0 .. $dbx->msgcount - 1) { my $msg = $dbx->get($i); push @emails, [ $msg->subject (), $msg->sender_name (), $msg->date_received ("%Y-%m-%d"), $msg->recip_name (), ]; } @emails = sort {$a->[2] cmp $b->[2]} grep {$_->[2] ge $lastEmail} @ema +ils; for my $email (@emails) { $email->[1] = $emailLists{$email->[1]} if exists $emailLists{$emai +l->[1]}; $email->[3] = $emailLists{$email->[3]} if exists $emailLists{$emai +l->[3]}; } my $MSWord; eval {$MSWord = Win32::OLE->GetActiveObject ('Word.Application')}; die "Word not installed" if $@; unless (defined $MSWord) { $MSWord = Win32::OLE->new ('Word.Application', sub {$_[0]->Quit;}) or die "Oops, cannot start Word"; } $MSWord->{Visible} = -1; # 0 = Don't watch what happens $MSWord->{DisplayAlerts} = 0; # 0 = do not prompt my $doc1 = $MSWord->Documents->Open ("$docDir\\Agenda template.doc"); # Select main view of the document # ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument $MSWord->ActiveWindow->ActivePane->View->{SeekView} = wdSeekMainDocume +nt; my $mainDoc = $MSWord->Selection (); # Get currently selected object $mainDoc->Find->ClearFormatting (); $mainDoc->Find->{Text} = "Correspondence since "; $mainDoc->Find->Replacement->{Text} = ""; $mainDoc->Find->{Forward} = 1; $mainDoc->Find->{Wrap} = wdFindStop; $mainDoc->Find->{Format} = 0; $mainDoc->Find->{MatchCase} = 0; $mainDoc->Find->{MatchWholeWord} = 0; $mainDoc->Find->{MatchWildcards} = 0; $mainDoc->Find->{MatchSoundsLike} = 0; $mainDoc->Find->{MatchAllWordForms} = 0; $mainDoc->Find->Execute (); $mainDoc->MoveRight ({Unit => wdCharacter, Count => 1}); $mainDoc->TypeText ({Text => $lastEmail}); $mainDoc->MoveStart ({Unit => wdTable, Count => 1}); for my $email (@emails) { $mainDoc->TypeText ({Text => $email->[2]}); $mainDoc->MoveRight ({Unit => wdCell, Count => 1}); $mainDoc->TypeText ({Text => $email->[1]}); $mainDoc->MoveRight ({Unit => wdCell, Count => 1}); $mainDoc->TypeText ({Text => $email->[3]}); $mainDoc->MoveRight ({Unit => wdCell, Count => 1}); $mainDoc->TypeText ({Text => $email->[0]}); $mainDoc->MoveRight ({Unit => wdCell, Count => 1}); } $MSWord->WordBasic->FileSaveAs ($docName); print "Found ", scalar @emails, " emails\n";

True laziness is hard work

Replies are listed 'Best First'.
Re: Word document email table populater
by Anonymous Monk on Nov 08, 2009 at 05:23 UTC
    Code is nice, but could be little modular and hence testable. Is there any way to get the XXXX.. folder name? That would be handy. See the registry entry HKEY_CURRENT_USER\Identities\account_ID. $lastEmail should be $lastEmailDate and can get the value from a subroutine, as one month earlier or a fixed value (if that is what you need). You are sorting by date, so use Hash in place of Array for storing, to make it more clear. Some command line options could be helpful. Hope that helps. Take it easy.