WinBatch Technical Support Forum

Archived Boards => COM Automation and dotNet => Topic started by: Mike Burgoon on March 17, 2015, 09:32:33 am

Title: OLE automating SpellCheck in Microsoft Word 2013
Post by: Mike Burgoon on March 17, 2015, 09:32:33 am
Hello, I am trying to automate the testing of Word using OLE.  The only issue I am having so far is trying to use OLE to automate the Spellcheck feature.  Basically, what the script does is launch word, create a new document, add some text with errors (3 lines), perform a spelling and grammar check on it, then close.  I am updating this script to use OLE instead of using push buttons and sendkeys.  I know that there may be issues below the check spelling piece, but I am only up to working on getting that to work at the moment.  I need the text that gets written to the document to change to the corrected form on the screen in the document.  At this point, that is all I need it to do.  Script is attached.



Title: Re: OLE automating SpellCheck in Microsoft Word 2013
Post by: td on March 17, 2015, 01:58:50 pm
Not sure which direction you are headed in but you can turn on Word's auto correct with the following

word.AutoCorrect.ReplaceText = @True

I don't know what happens when auto correct can't guess the spelling.

The CheckSpelling method is member of the 'Word.Application' object (some Application object members are also exposed by the _Document object) and is expecting  text as a parameter. It returns a true value if the word is spelled correctly and 0 if the word is not spelled correctly.

The GetSpellingSuggestions method returns a collection of suggestions for a misspelled word.

Code: Winbatch
   
objSuggestions = word.GetSpellingSuggestions("aracnifobia")
if objSuggestions.Count
   ForEach objS in objSuggestions
       Pause("aracnifobia", objS.Name)
   next
endif
 


You can find the complete MSFT Word programming model here:

https://msdn.microsoft.com/en-us/library/office/ff837519.aspx (https://msdn.microsoft.com/en-us/library/office/ff837519.aspx)
 
Title: Re: OLE automating SpellCheck in Microsoft Word 2013
Post by: Mike Burgoon on March 18, 2015, 06:12:46 am
Thanks for the help.  the autocorrect feature is on by default in our environment, but it does not catch the spelling and grammar mistakes that are included in our test.  the biggest issue is this needs to be fully automated, no user interaction.  I will keep playing with it.  I am re-learning how to using OLE in winbatch (the last time I did it was probably close to 8 or 9 years ago) and am very rusty.  My Excel script works like a dream.

Another issue I am having though is getting the file to save.  from the same script in the first post, I have the following changes at the bottom:

wdFormatDocument=0
docFilename = "C:\MyWordFile.docx"
If FileExist(docFilename) Then FileDelete(docFilename)
Document.SaveAs(docFilename, wdFormatDocument)

It won't save.  I have tried several variations, and nothing works to save the document.  My excel script saves just fine.  I have tried mirroring that as well with Document.SaveAs(docFilename) and that does not work either.  Any help on that would be great as well.
Internet searches have not helped much yet.

Thanks
Title: Re: OLE automating SpellCheck in Microsoft Word 2013
Post by: Mike Burgoon on March 18, 2015, 07:07:33 am
I was able to solve my saving issue.

Document.SaveAs2(docFilename)

No just to figure out the spell checker.  That is the important issue I need to solve.

thanks
Title: Re: OLE automating SpellCheck in Microsoft Word 2013
Post by: td on March 18, 2015, 07:55:57 am
Thanks for the help.  the autocorrect feature is on by default in our environment, but it does not catch the spelling and grammar mistakes that are included in our test.  the biggest issue is this needs to be fully automated, no user interaction.  I will keep playing with it.  I am re-learning how to using OLE in winbatch (the last time I did it was probably close to 8 or 9 years ago) and am very rusty.  My Excel script works like a dream.

You would need to create your own correction algorithm by forcing the selection of a Word suggestion or some other approach.  I don't know of a way to force Word to automagically correct every error.
Title: Re: OLE automating SpellCheck in Microsoft Word 2013
Post by: Mike Burgoon on March 18, 2015, 09:13:06 am
I am trying to figure out how to just select the FIRST response that is returned and replace that in the document.  Also, how to do the same with the third sentence and the grammar mistake.

This is only a test to make sure that the autocorrect, spelling, and grammar checks are working correctly in Word.  The text will always be what is written in the script, then that text is checked.