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

Hi, I have the following code that takes my input string ($word) and returns and prints a list of synonyms from Microsoft Word:
#!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); # Word and language to search for my $word="quick"; my $language=$wd->{wdEnglishUS}; # Get synonyms from MSWord my $synonyms=$MSWord->SynonymInfo( {Word => $word, LanguageID => $language})->SynonymList(1); # Print them out... foreach (@{ $synonyms }) { print $_."\n" };
In the above example $word="quick" and the script works fine; however, if I were to use a word for which there are no synonyms in MS Word (eg. $word="xxx"), then when the script runs it returns with this error:
OLE exception from "Microsoft Word": One of the values passed to this method or property is out of range. Win32::OLE(0.1403) error 0x800a16d3 in METHOD/PROPERTYGET "SynonymList" at C:\syn.pl line 18
I'm not sure how to handle such an error. Ideally, I would like the script to just print out "No synonym suggestions". Can anyone help?

Thanks,
Adam

Replies are listed 'Best First'.
Re: Problem using Win32::OLE with MS Word
by Trix606 (Monk) on Nov 11, 2005 at 04:06 UTC
    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. :)

      I tried your solution and it worked beautifully but I get another puzzling error if Iadd the word "pieces" to the array. eg.

      my @words=("quick","xxx","pieces");


      To hopefully get more of an indication of what the problem is, I uncommented your line:

      Win32::OLE->Option(Warn => 0); # Ignore OLE Errors

      The error I get when I run the script is this:

      quick: rapid quick: fast quick: speedy quick: swift quick: nippy xxx: Do not have any synonyms. OLE exception from "Microsoft Word": One of the values passed to this method or property is out of range. Win32::OLE(0.1403) error 0x800a16d3 in METHOD/PROPERTYGET "SynonymList" at C:\adam2.pl line 22 Can't use an undefined value as an ARRAY reference at C:\adam2.pl line + 26.


      The thing is if I use the Thesaurus in MS Word directly on the word "pieces" it does return one entry, that being "piece".

      Any idea why then the script reports that value as being out of range?
        Actually, looking at this more closely:

        >>The thing is if I use the Thesaurus in MS Word
        >>directly on the word "pieces" it does return one
        >>entry, that being "piece".

        ...that is not true. In fact, the Thesaurus returns "piece" and suggests this as a related word(ie. Replace with Related Word) and not as a synonym (ie. Replace with Synonym).

        So in this case there is no suggested synonym but how would I go about coding for that (I don't want related words - just a list of matching synonyms returned).

        Thanks, Adam
Re: Problem using Win32::OLE with MS Word
by jplindstrom (Monsignor) on Nov 11, 2005 at 00:10 UTC
    my $synonyms; eval { $synonyms=$MSWord->SynonymInfo({Word => $word, LanguageID => $lang +uage})->SynonymList(1); }; if($@) { print "Nope\n"; } else { foreach (@{ $synonyms }) { print $_."\n" }; }

    Type "perldoc -f eval" and "perldoc -f die" at a command prompt to see more information about this.

    /J

Re: Problem using Win32::OLE with MS Word
by Discipulus (Canon) on Nov 11, 2005 at 09:40 UTC
    hello Adam,

    this is not a solution but I proved the last snipped and, 'cause I'm italian I put wdItalian instead of wdEnglishUS and ...

    the result for xxx change !!(another happy redmond jack in the box funny surprise!!) from the error reported above to the correct and expected value:
    'none.'

    I think u easier learn italian than solve a wd issue

    hope this help lor*