Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Win32::OLE Outlook Error

by cormanaz (Deacon)
on Oct 09, 2014 at 00:13 UTC ( [id://1103245]=perlquestion: print w/replies, xml ) Need Help??

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

Greetings Bros. I am working on some Outlook automation and have started out by just trying to print the contents of some message fields like so:
#!/usr/bin/perl -w use strict; use Win32::OLE; $| = 1; # use existing instance if Outlook is already running, or launce a new + one my $ol; eval {$ol = Win32::OLE->GetActiveObject('Outlook.Application')}; die "Outlook not installed" if $@; unless (defined $ol) { $ol = Win32::OLE->new('Outlook.Application', sub {$_[0]->Quit;}) or die "Oops, cannot start Outlook"; } my $mailbox = seekFolder($ol->Session, 'foo@bar.com'); my $folder = seekFolder($mailbox, 'Inbox'); my @fields = qw(SenderName SenderEmailAddress ReplyRecipientName +s SenderEmailType SentOn ReceivedTime MessageClass Siz +e Subject To CC BCC Unread InternetCodepage Impo +rtance EntryID ConversationIndex ConversationTopic Class + BodyFormat ); my $end = $folder->Items->Count; for (my $i = $end; $i > $end-5; $i--) { print "=========================================================== +===========\n"; foreach my $k (@fields) { print "$k: ".$folder->Items->Item($i)->{$k} . "\n"; } } Win32::OLE->FreeUnusedLibraries(); sub seekFolder { my $obj = shift; my $target = shift; for (my $i = 1; $i <= $obj->Folders->Count; $i++) { if ( $obj->Folders->Item($i)->Name eq $target ) { return $obj->Folders->Item($i); } } }
It works fine for the most part, but when there is an empty field in the message object I get an OLE error like:
Win32::OLE(0.1709) error 0x8002000e: "Invalid number of parameters" in METHOD/PROPERTYGET "InternetCodepage" at C:\recover\boxcutter\n +ewtry\test.pl line 27. Use of uninitialized value in concatenation (.) or string at C:\recove +r\boxcutter\newtry\test.pl line 27.
I tried to avoid this by enclosing the print statement in an if defined clause, but this does not change anything. Anyone know how to avoid this error?

Replies are listed 'Best First'.
Re: Win32::OLE Outlook Error
by Anonymous Monk on Oct 09, 2014 at 06:57 UTC

    Anyone know how to avoid this error?

    Sure, satisfy the API and/or trap the error

    ... my $end = $folder->Items->Count; for my $ix ( reverse $end-5 .. $end ){ print "==========" x 7, "\n"; my $item = $folder->Items->Item( $ix ); for my $field ( @fields ){ ## my $val = eval { $item->{ $field } }; my $val; eval { $val = $item->{ $field }; 1 } or do { warn "uhoh $folder $ix $item $field : $@ "; next; }; print "$field: $val\n"; } } ... my $count = $obj->Folders->Count; for my $ix ( 1 .. $count ){ my $item = $obj->Folders->Item($i); return $item if $item->name eq $target }
      Thanks. I suppose I should have thought of that. The weird thing is that it has stopped happening with the original code, for reasons unknown :-/

      UPDATE: Well I spoke too soon. It is happening again, which is weird in itself, and suggests the cause is not an empty field. I don't understand the message about parameters, because the function call has the same number of parameters when I get the error and when I don't.

      Also eval does not trap the error -- it does so for the perl statement, but OLE still throws the error.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (1)
As of 2024-04-25 05:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found