Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VERY SIMPLE use of MS WORD Spell checker 1

Status
Not open for further replies.

JohnWMAC

Programmer
Aug 22, 2018
9
US
I found what I thought was the perfect solution to spellchecking individual words using automation of MS WORD from VFP9 (SP2). When I started using it, it worked PERFECTLY, then, all of a sudden, it quit. "QUIT" meaning WORD was always returning a TRUE condition even when the word in question was obviously misspelled. Details follow:

WORD - MS Office 365
VFP9 (SP2)
Windows 10

This code is in the INIT procedure of my Form:

PUBLIC oWord
oWord=CREATEOBJECT("Word.Application")
(WORD always fires up perfectly)

Then I have a TEXTBOX on the form in which the following code appears in the LostFocus event:

lCorrect = .F.
Store "JNHYGT" to answer
lCorrect = oWord.CheckSpelling(answer)

set step on

IF lCorrect
(I ALWAYS land here)
ELSE
MESSAGEBOX(ALLTRIM(answer) + " is NOT a valid word",0)
ENDIF

lCorrect ALWAYS comes back as TRUE.

Now it gets interesting - When I run this in Debug mode and STOP the execution where indicated, and then go to the COMMAND window and call WORD's spellchecker with the same "JNHYGT", it comes back false!!
I reinstalled VFP9 which did nothing. As I said, when I first wrote this code, it worked PERFECTLY and then all of a sudden, it started to fail. I assume you may think it is an environmental issue on my computer which is a reasonable assumption so I am looking for trouble shooting suggestions rather than a simple solution.
 
John,

Sorry I can't suggest a solution. When I run your code, I see exactly the same result as you are seeing - including the same behaviour in debug mode.

I can only add that re-installing VFP is very rarely a solution to this type of problem. In this case, VFP appears to be functioning perfectly; it is the return value from Word that is the problem.

I hope someone else can come up with an answer.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Thanks for the response Mike.

Just after I wrote my initial post, I kept fiddling with this and (don't ask me why) but if you send the argument to WORD in lower case, it works perfectly! Haven't followed this up, but MAYBE WORD considers upper case to be sacred (always correct) in some fashion. I sent the word "ELIZABETH" and it came back true. The word "elizabeth" comes back false. Sounds like WORD is the "problem" if I can call it a problem. I am also going to test with previous versions of WORD just to see if WORD 365 is the issue. Bottom line: lower case is a great solution for MY issue. It may or may not be a solution for others.
 
John,

I don't think automation works with Office 365 - no COM interface. We also use Word for spell-checking on Windows 10 and it works great with versions 10, 14, 16, 19, and 21. But, only for desktop installs.

Are you sure you can get an object reference to Word with Word 365?
 
It would make sense. Uppercase are start of a sentence, but standalone single upper case words can be names, and nothing is disallowed (well, ask your registry office, but see celebrities children names), all uppercase even is considered an abbreviation and not spellchecked. It works just the same interactively when you write texts in Word. Or in Outlook (where word spell checking also is a default).

Chriss
 
Yes, that's the answer. There is a setting in Word, under Proofing Options, which says "When correcting spelling in Microsoft Office programs ... Ignore words in UPPERCASE".

Un-tick that option and it should work fine.

There are also options that say "Ignore words that contain numbers" and "Ignore Internet and file addresses".

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Vernpace:

I BELIEVE Office 365 comes in both a WEB version and a desktop version. I am running the desktop version and, when you start WORD, it clearly calls itself WORD 365. I agree with your assessment that the WEB version cannot accomplish automation.
 
Vernpace
Yes, I understand how to do this....Thanks!
 
Hi John,

Using Word for spellchecking is an interesting idea.
How about using an editbox for input? How could we run spellchecking the entered text?
Is it possible to change the spellchecker's language?

Regards, Gerrit
 
Gerrit,

simply look into the documentation. You can specify the dictionary to use. Word also autodetects languages and even spellchecks texts with passages in another language. Which happens quite a lot with English words or phrases in other languages texts.

The obvious requirement to use it is that customers have their own MS Office license. At least Word, but does MS even sell single Office Apps anymore?

But yes, the automation server of Word or Office are not only there to create spreadsheets, mails and documents.

Chriss
 
Hi Chris,

I have used Word automation for many years and I get the concept of course.

Checking each word seperately is not quite what users will expect from a spellchecker these days.
I haven't found anything yet about using MS Word with a VFP editbox. I'm pretty sure it's not in Word's online help (at least not in mine).
So what documentation are you refering to?

Regards, Gerrit
 
My particular requirement was to spell check a single word but you certainly can pass multiple words in a given call to the spell checker. Text box, edit box, should not matter.
 
Hi John,

If I use a long text from an edit box, how should I know which word was misspelled? How would you make that visible? I’m still confused about this concept.

Regards, Gerrit
 
Gerrit,

of course you wont pass over a reference to an editbix, but the whole text in it. Well, or run spellcheck on text you set into a document.

PS: obviously you then would like more info than just .T./.F., if a whole text is checked. Tamars article mentions GetSpellingSuggestions, I think you can let Word spellcheck a whole document and get positions of spelling and grammar errors. They become objects of the document just like the paragraphs and words themelves. Just dig into the object model of Word, it's fully documented.

PPS: Of course the VFP editbox has limited capabilities to use coloring, squiggly underlined words or even just normal underlined words. You go to RTF for that matter, or embed a word document as OLE control.

Chriss
 
Gerrit,

It can be done. However, inline spell-checking is a different animal from checking an entire document. We use Richtext as shown below:

Untitled_plcmnj.png


Here is "Part" of the code:

Code:
*!*    Quality check
*!*    To be used with RTF spell-checker
PROCEDURE ShowRTFSpellingErrors(toRichText AS Object) AS Integer
   LOCAL lcText, lcDelimiters, lcWord, liCount, liErrors, liStartRange, loWord, IsError, i

   TRY
       lcText = toRichText.Text
   CATCH WHEN .T.
       IsError = .T.
   ENDTRY

   IF IsError
      RETURN -1
   ENDIF

   lcDelimiters = ""

TEXT TO lcDelimiters TEXTMERGE NOSHOW
~!@#$%^&*()_-+={}[]|\/:;<>?,"
ENDTEXT

   lcDelimiters = lcDelimiters + SPACE(1) + CHR(13) + CHR(10) + CHR(9) + CHR(0)
   liCount = GETWORDCOUNT(lcText, lcDelimiters)

   IF liCount = 0
      RETURN -2
   ENDIF

   loWord = CREATEOBJECT("Word.Application")

   liErrors = 0
   liStartRange = 0

   FOR i = 1 TO liCount
       lcWord = GETWORDNUM(lcText, i, lcDelimiters)

       IF NOT loWord.CheckSpelling(lcWord)
          IF LEFT(lcWord, 1) = "."
             lcWord = SUBSTR(lcWord, 2)
          ENDIF

          IF RIGHT(lcWord, 1) = "."
             lcWord = SUBSTR(lcWord, 1, (LEN(lcWord) - 1))
          ENDIF

          liStartRange = ShowRTFSpellingWord(toRichText, lcWord, liStartRange, @liErrors)

       ENDIF
   ENDFOR

   loWord.Quit
   RELEASE loWord
   toRichText.SelStart = 0

   SYS(1104)

   RETURN liErrors

ENDPROC


*!*    Quality check
*!*    Used with RTF spell-checker function ShowRTFSpellingErrors
FUNCTION ShowRTFSpellingWord(toRichText AS Object, tcWord AS String, tiStartRange AS Integer, tiErrors AS Integer) AS Integer
   LOCAL lcStruct, loFindTextEXW, loCharFormat2, liSelStart, liSelLength

   loFindTextEXW = CREATEOBJECT("findtextexw")
   loFindTextEXW.chrg(1).cpMin = tiStartRange
   loFindTextEXW.chrg(1).cpMax = -1
   loFindTextEXW.lpstrText = tcWord
   lcStruct = loFindTextEXW.GetString()

   IF SendMessageCRef(toRichText.hWnd, EM_FINDTEXTEXW, (FR_DOWN + FR_MATCHCASE + FR_WHOLEWORD), @lcStruct) > -1
      loFindTextEXW.SetString(lcStruct)
      liSelStart = loFindTextEXW.chrgText(1).cpMin
      liSelLength = loFindTextEXW.chrgText(1).cpMax - liSelStart
      toRichText.SelStart = liSelStart
      toRichText.SelLength = liSelLength

      loCharFormat2 = CREATEOBJECT("charformat2")
      loCharFormat2.cbSize = loCharFormat2.SizeOf()
      loCharFormat2.dwMask = CFM_BACKCOLOR
      loCharFormat2.crBackColor = 13106941

      SendMessageC(toRichText.hWnd, EM_SETCHARFORMAT, SCF_SELECTION, loCharFormat2.GetString())
      tiErrors = tiErrors + 1
      RETURN (loFindTextEXW.chrgText(1).cpMax + 1)
   ELSE
      RETURN tiStartRange
   ENDIF

ENDFUNC
 
If I use a long text from an edit box, how should I know which word was misspelled?

You don't. Once you start the spell check, Word takes over the user interaction. It is Word that advises the user that the word is incorrect, and it is Word that offers suggestions for corrections - just as if the user was using Word itself. You only need to store the corrected text back in the edit box.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
This is not to detract from the code that Vernspace posted, but here is my own Word spell check code, for what it's worth:

Code:
* Spellchecks the contents of the edit field. 

LOCAL loDoc, llWasVisible
 
* The OpenWord() method is responsible for instantiating Word, but you could just
* as easily do that here instead.
IF NOT thisform.OpenWord() 
	MESSAGEBOX("Cannot perform the spell check." + CHR(10) + CHR(10) + ;
		"Unable to open Microsoft Word", 48, gcShortTitle)
		
	RETURN
ENDIF 		


* Go ahead and do the spell check 
WITH thisform.oWord

	* Open a new doc within Word
	loDoc = .Documents.add
	
	* Copy the text to the new doc
	loDoc.Range(0,0).InsertAfter(thisform.edtMemo.Value)
            && put the name of the edit box here
	
	* Make it visible
	llWasVisible = .visible		&& save the visible state
	.visible = .T.
	.WindowState = 0			&& normal window
	.Activate
	
	* Call the spell checker
	loDoc.CheckSpelling

	* Copy the text back 
	thisform.edtMemo.Value = loDoc.content.text
	
	* Close the Word doc (without prompting to save it)
	loDoc.close(0)

	* Minimise Word (otherwise it might still be on top at this point)
	.WindowState = 2	
	
	IF NOT llWasVisible
		.visible = .F.
	ENDIF 
	
	MESSAGEBOX("Spellcheck completed", gcShortTitle)
	
ENDWITH

I've used this on several projects over the years, and it has served me well.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top