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

In reply to Word document email table populater by GrandFather

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.