I tried jplindstrom's solution but could not get eval to trap the error.

Try this:

#!perl -w # Uses use strict; use Win32::OLE; use Win32::OLE::Const; # Create MSWord object and load constants my $MSWord = Win32::OLE->new('Word.Application', 'Quit') or die "Could not load MS Word\n"; my $wd=Win32::OLE::Const->Load($MSWord); # Words and language to search for my @words= ("quick","xxx"); my $language=$wd->{wdEnglishUS}; Win32::OLE->Option(Warn => 0); # Ignore OLE Errors # Get synonyms from MSWord for my $word (@words) { my $synonyms=$MSWord->SynonymInfo({Word => $word, LanguageID => $l +anguage}); my $OLEerror = Win32::OLE->LastError(); unless ($OLEerror) { if ($synonyms->Found) { foreach (@{ $synonyms->SynonymList(1) }) { print "$word: $ +_.\n" }; } else { print "$word: Do not have any synonyms.\n"; } } else { print "OLE Error: $OLEerror\n"; } } Win32::OLE->Option(Warn => 1); # Reset to default OLE Error Handling

This calls the Found method on the synonyms object before trying to access the SynonymList.

Update: Modified program to handle missing thesaurus. After seeing Discipulus' post I tried the Italian thesaurus which caused the program to fail because I don't have the Italian thesaurus DLL installed.

Basically you're ignoring the error from OLE (Win32::OLE->Option(Warn => 0)) and checking for it yourself with Win32::OLE->LastError().

Note that the last line resets things so you'll get any subsequent OLE errors.

Update: Added comments to highlight <c>Win32::OLE->Option<c/> use. Changed last line to reset to default setting for OLE Errors.

Now attempting to move on. :)


In reply to Re: Problem using Win32::OLE with MS Word by Trix606
in thread Problem using Win32::OLE with MS Word by adam2005

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.