use strict; use Win32::OLE; use Win32::OLE::Const 'Microsoft Word'; # Win32::OLE->Option(Warn => 0); my $word = Win32::OLE->new('Word.Application', 'Quit') or die; # $word-> {DisplayAlerts} = 0; # $word-> {WindowState} = 0; $word-> {Visible} = 0; my $doc = $word->Documents->Add(); my $range = $doc->{Content}; $range->{Text} = 'Analyser'; $range->InsertParagraphAfter(); my $paras = $doc->Paragraphs; foreach my $para (in $paras) { print ">> " , $para->Range->{Text}; my $text=$para->Range->{Text}; $para->Range->{LanguageID}=2057; my @errors=&spellcheck($word,$text); my @guesses; if($#errors==0) { foreach my $word(@errors) { my $guesses = $word->{'guesses'}; @guesses = @$guesses; } print "[@guesses]\n"; } else { print "[@guesses]\n"; } } <>; $doc-> Saved(0); $doc-> Close(0); $word-> Quit(); sub spellcheck { my $self = shift; my $text = shift; my $msword = $self; my @errors; while ($text =~ m/(\S+)\b/g) { my $offset = pos($text) - length($1) + 1; my $term = { term => $1, offset => $offset }; # If is correctly spelled proceed to the next word next if $msword->CheckSpelling($term -> {term}); # Otherwise look for spelling suggestions my $suggestions = $msword->GetSpellingSuggestions($term -> {term},LanguageID => 2057); # No suggestions: if (!$suggestions || !$suggestions -> {Count}) { $term -> {type} = 'none'; $term -> {guesses} = []; push @errors, $term; # Some suggestions: } else { my @suggest; foreach (in $suggestions) { push @suggest, $_->{Name}; } $term -> {type} = 'guess'; $term -> {guesses} = \@suggest; push @errors, $term; } } return @errors; }