Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Win32 - M$ Outlook and Perl.

by ChunLi (Novice)
on May 06, 2002 at 20:26 UTC ( [id://164439]=perlquestion: print w/replies, xml ) Need Help??

ChunLi has asked for the wisdom of the Perl Monks concerning the following question:

Hi all. I have a question for the monks that have Win32 and OLE experience. I have been searching for a method of using perl to enumerate all of the messages and attachments in an outlook client. Thus far, I have seen several examples or perl and outlook, but none seem to fit the need. Can someone please point me in the direction of some example source code? Likewise, is there a technique, or package for perl that can read a PST file? I thank all of you for any comments, or suggestions in advance.

Replies are listed 'Best First'.
(RhetTbull) Re: Win32 - M$ Outlook and Perl.
by RhetTbull (Curate) on May 06, 2002 at 21:42 UTC
    This is a bit ugly but should get you started.
    #!/cygdrive/c/Perl/bin/perl.exe use strict; use warnings; use Win32::OLE; use Win32::OLE::Const 'Microsoft Outlook'; $|++; #what properties do we want to get from Outlook? my @properties = ( "SenderName", "To", "Subject", "Body" ); #get an Outlook object my $outlook; $outlook = Win32::OLE->new('Outlook.Application'); die unless $outlook; #get the Inbox folder my $namespace = $outlook->GetNamespace("MAPI"); my $folder = $namespace->GetDefaultFolder(olFolderInbox); my $items = $folder->Items; print STDERR "Folder: ", $folder->Name,"\n"; print STDERR "Total entries: ",$items->Count,"\n"; #print out the header print join(",",@properties),"\n"; #loop through the items, printing out each one for my $itemIndex (1..$items->Count) { my $message = $items->item($itemIndex); next if not defined $message; my $attach = $message->Attachments(); my @entry; #todo: the following line causes warnings on "Read:" read receipt +messages push @entry, defined($message->{$_}) ? $message->{$_} : 'undef' fo +reach (@properties); if (defined $attach) { #if there's attachments, print out the message info and save t +he attachments print join(",",@entry),"\n"; print "Attachment count == ",$attach->Count,"\n"; for my $attach_index (1..$attach->Count) { my $attachment = $attach->item($attach_index); my $filename = $attachment->Filename; print "Attachment == ",$filename,"\n"; my $saveas = 'c:\temp\foo'; print "saving attachment '$filename' to '$saveas'...\n"; $attachment->SaveAsFile($saveas); warn "error saving attachment $filename to $saveas" if !-e + $saveas; } } }

      Very nice.
      But I'd change the following: my $saveas = 'c:\temp\foo';
      to: my $saveas = "c:/temp/foo/".$filename;
      so you don't overwrite the attachments. Of course, you may have wanted to do that.


      Matthew Musgrove
      Who says that programmers can't work in the Marketing Department?
      Or is that who says that Marketing people can't program?
        Actually, that was just for demonstration purposes. I realized I was overwriting 'foo' but then again, I don't name anything I intend to keep 'foo' ;-)
      RhetTbull , you rock man !! thanks a lot. this code eased my task very much ...
      Who do I read mail in subfolders: inbox/subfolder
        This will recursively print out the names of all subfolders in Inbox. You could combine this with the code above to get the messages in each subfolder.
        use strict; use warnings; use Win32::OLE; use Win32::OLE::Const 'Microsoft Outlook'; $|++; #get an Outlook object my $outlook; $outlook = Win32::OLE->new('Outlook.Application'); die unless $outlook; #get the Inbox folder my $namespace = $outlook->GetNamespace("MAPI"); my $folder = $namespace->GetDefaultFolder(olFolderInbox); my $items = $folder->Items; print STDERR "Folder: ", $folder->Name,"\n"; print STDERR "Total entries: ",$items->Count,"\n"; print_folders($folder); sub print_folders { my $folder = shift; print "Folder: " . $folder->Name . "\n"; if ($folder->Folders->Count) { foreach my $i (1..$folder->Folders->Count) { print_folders($folder->Folders($i)); } } }
Re: Win32 - M$ Outlook and Perl.
by Mr. Muskrat (Canon) on May 06, 2002 at 20:59 UTC

    Updated.

    Anytime I am trying to use perl and OLE to access M$ products, I start by creating a macro that does exactly what I want the perl program to do. Then I start translating the macro into perl. I've been considering creating a translator but I just never seem to have the time. Most of the work of translating it can be done with some regular expressions. It's the odd situations that the translator is actually required for.
    For example, I wanted to setup my page for printing and I created a macro to do it. Here is a small snippet of that macro:

    With ActiveSheet.PageSetup .PrintTitleRows = "$1:$2" .PrintTitleColumns = "" .PrintArea = "$B:$I" End With
    So I save that little snippet in a file called mymacro.txt. And then I run this code on it:
    my $pl = ""; open(TXT, "<mymacro.txt"); while (<TXT>) { s/\./->/; s/=/=>/; s/"/'/g; s/\s+End With/);/; s/\s+With (\w+)->(\w+)/\$Range = \$$1->$2;\nwith (\$Range/i; s/(.*[^;])\n/$1,\n/; $pl .= $_; } close(TXT); open(TXT, ">mymacro.pl"); print TXT $pl; close(TXT);
    and the contents of mymacro.pl are:
    $Range = $ActiveSheet->PageSetup; with ($Range, PrintTitleRows => '$1:$2', PrintTitleColumns => '', PrintArea => '$B:$I', );
    Not all macros will translate this easily though.

    Matthew Musgrove
    Who says that programmers can't work in the Marketing Department?
    Or is that who says that Marketing people can't program?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://164439]
Approved by VSarkiss
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-12-07 14:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which IDE have you been most impressed by?













    Results (50 votes). Check out past polls.