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

Re: Problem using Win32::OLE with MS Word

by Trix606 (Monk)
on Nov 11, 2005 at 04:06 UTC ( [id://507622]=note: print w/replies, xml ) Need Help??


in reply to Problem using Win32::OLE with MS Word

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. :)

Replies are listed 'Best First'.
Re^2: Problem using Win32::OLE with MS Word
by adambrad (Initiate) on Nov 12, 2005 at 11:59 UTC
    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
        Solved. Just had to replace this:
        if ($synonyms->Found)

        ....with this:
        if (($synonyms->Found) && ($synonyms->SynonymList(1)))

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-04-20 05:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found