in reply to Problem using Win32::OLE with MS Word
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 | |
by adambrad (Initiate) on Nov 12, 2005 at 12:23 UTC | |
by Anonymous Monk on Nov 14, 2005 at 10:57 UTC |