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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Spell Check in Vis Fox

Status
Not open for further replies.

keepingbusy

Programmer
Apr 9, 2000
1,470
GB
Hi All

Found this interesting code (below) from Microsoft regarding spell checking in Visual FoxPro

Q271819 - HOWTO Spell Check in Visual FoxPro with Microsoft Word

My question is, how can I adapt this to remove the createobject and use my own form with an edit box relating to a memo field for checking?

The line PROCEDURE Init
THIS.edtTextToCheck.VALUE = "Thhis text has mistakees in it. We will seend " + ;
"it to Word and have it cheked."

will need to be changed to accept my memo field (named info2) from my dbf (filename newmembs.dbf) (I assume!)

*!*********** START CODE ***********

PUBLIC oform1

oform1=CREATEOBJECT("form1")
oform1.Show
RETURN

*************************************************
*
*
DEFINE CLASS form1 AS form

Height = 250
Width = 375
ShowWindow = 2
AutoCenter = .T.
BorderStyle = 2
Caption = "VFP/Word Spell Checking"
MaxButton = .F.
Name = "Form1"

ADD OBJECT edttexttocheck AS editbox WITH ;
Height = 163, ;
Left = 23, ;
Top = 21, ;
Width = 332, ;
Name = "edtTextToCheck"

ADD OBJECT cmdcheckspelling AS commandbutton WITH ;
Top = 207, ;
Left = 115, ;
Height = 27, ;
Width = 149, ;
Caption = &quot;\<Check Spelling&quot;, ;
Name = &quot;cmdCheckSpelling&quot;

PROCEDURE findword
*~~~~~~~~~~~~~~~~~~~~~
* PROCEDURE FindWord
*
* AUTHOR: Trevor Hancock, , Microsoft Corporation
* CREATED : 08/22/00 11:50:32 AM
*
* ABSTRACT: Locates an installation of MS Word using the FindExecutable API
* Creates a file with a .doc extension, checks the association on that
* file using FindExecutable, then deletes the file. FindExecutable returns
* the full, null terminated path to the application associated with
* .doc files (in this case).
* ACCEPTS: Nothing
* RETURNS: Full path to the application associated with .doc files on this machine.
*~~~~~~~~~~~~~~~~~~~~~

LOCAL lcPath, lcResult, lcFileName, llRetVal, ;
lcCurDir, lnFileHand, lcWordPath

lcPath = SPACE(0)
lcResult = SPACE(128)
llRetVal = .F.
*!* Determine the DIR this form is running from. JUSTPATH() and ADDBS()
*!* could be used here instead (if using VFP6), but this code will work in any VFP version.
lcCurDir = SUBSTR(SYS(16,0),ATC([ ],SYS(16,0),2)+1)
lcCurDir = SUBSTR(lcCurDir,1,RAT([\],lcCurDir))

lcFileName = lcCurDir + SYS(3) + [.doc]

*!* Create a file with a .doc extension.
*!* Could use STRTOFILE() here in VFP6.
lnFileHand = FCREATE(lcFileName,0)
= FCLOSE(lnFileHand)

DECLARE INTEGER FindExecutable IN shell32 STRING @lcFilename, ;
STRING @lcPath , STRING @lcResult

*!* Determine the file association on .DOC files
IF FindExecutable(@lcFileName, @lcPath, @lcResult) > 32
*!* Strip off trailing chr(0)
lcWordPath = UPPER(SUBSTR(lcResult,1,LEN(ALLTR(lcResult))-1))
IF [WINWORD] $ lcWordPath
llRetVal = .T.
ENDIF
ENDIF
*!* Clean up after ourselves
ERASE (lcFileName)
RETURN llRetVal
ENDPROC

PROCEDURE Destroy
IF TYPE([goWord]) = [O]
IF TYPE([goWordDoc]) = [O]
goWordDoc.SAVED = .T.
goWordDoc.CLOSE
ENDIF
goWord.QUIT
ENDIF
RELEASE goWord, goWordDoc
ENDPROC

PROCEDURE Init
THIS.edtTextToCheck.VALUE = &quot;Thhis text has mistakees in it. We will seend &quot; + ;
&quot;it to Word and have it cheked.&quot;

ENDPROC

PROCEDURE cmdcheckspelling.Click
*~~~~~~~~~~~~~~~~~~~~~
* PROCEDURE cmdcheckspelling.CheckSpelling
*
* AUTHOR: Trevor Hancock, Microsoft Corporation
* CREATED : 08/22/00 12:03:46 PM
*
* ABSTRACT: Automates MS Word to check the spelling of text in
* THISFORM.edtTextToCheck
* ACCEPTS: Nothing
* RETURNS: Nothing
*~~~~~~~~~~~~~~~~~~~~~

IF TYPE([goWord]) # [O] && Check if you have already instantiated Word

IF !THISFORM.FindWord() && You don't have Word up, so let's locate it.
MESSAGEBOX([Microsoft Word is either not installed or is incorrectly registered.], + ;
0,[Word Start-Up Failed])
RETURN .F.
ENDIF

*!* Change the mouse pointer for all form controls to indicate processing (opening Word)
WITH THISFORM
.cmdCheckSpelling.MOUSEPOINTER = 11
.edtTextToCheck.MOUSEPOINTER = 11
.MOUSEPOINTER = 11
ENDWITH

PUBLIC goWord, goWordDoc && Public vars for Word and Document1 in Word.
goWord = CREATEOBJECT([WORD.APPLICATION]) && Create Word
WITH goWord
.WINDOWSTATE= 0 && wdWindowStateNormal (needs to be Normal before you can move it)
.MOVE(1000,1000) && Move the window out of view
goWordDoc = .Documents.ADD
ENDWITH

*!* Change mouse pointers back
WITH THISFORM
.cmdCheckSpelling.MOUSEPOINTER = 0
.edtTextToCheck.MOUSEPOINTER = 0
.MOUSEPOINTER = 0
ENDWITH

ENDIF

WITH goWordDoc
.Content.TEXT = ALLTRIM(THISFORM.edtTextToCheck.VALUE)
.ACTIVATE
IF .SpellingErrors.COUNT > 0
.CHECKSPELLING
ELSE
=MESSAGEBOX([Spell check complete. No errors found],0,[Spell Check])
ENDIF
*!* For some reason, Word likes to make itself visible here. Keep it hidden...
goWord.VISIBLE = .F.
THISFORM.edtTextToCheck.VALUE = .Content.TEXT
ENDWITH
ENDPROC

ENDDEFINE
*
*
**************************************************
*!*********** END CODE ***********


Look forward once again to any replies

KB
When everything else is said and done, there will be nothing else to say or do! God Bless everyone....
 
word spell checking if it has found any errors will show a correction window for the user to select the corrections. if this is what you are talking about, then you can not remove this with a substatute from of your own. Attitude is Everything
 
Create a function like CheckSpelling and put the following in it:
Code:
PARAMETERS cText
LOCAL oWord,bRes
&& Create a Word Instance
oWord = createobject(&quot;Word.application&quot;)
&& Add Document
WITH oWord.Documents.Add
  && populate Document with parametertext
  .content.text = cText
  && Spellcheck
  IF .SpellingErrors.Count > 0
    && if there are errors, display correction-dialog
    .CheckSpelling
    && Get corrected Text after correction-dialog
    cText = .content.text
  ENDIF
ENDWITH
&& ensure that word doesn't save the document
oWord.quit(.F.)
&& return the corrected text
RETURN cText

Then you can call it this way:
Code:
messagebox(CheckSpelling(&quot;thos ist wrong speld&quot;))
If there are many checks to do it would make sense to create a public Word-instance and to reference this one in this function.

hope this helps
Andreas
 
as stated if you want to use the word spell checker to allow user to correct the words you have to allow the correcton window to be visible.

Schweiger
yes you can achive the check but no corrections window, but I belive you have the syntx wrong.

word has to ways to call the spell check
CheckSpelling
Begins a spelling check for the specified document or range. If the document or range contains errors, this method displays the Spelling and Grammar dialog box (Tools menu), with the Check grammar check box cleared. For a document, this method checks all available stories (such as headers, footers, and text boxes).


CheckSpelling('text to check')
Checks a string for spelling errors. Returns True if the string has no spelling errors


Attitude is Everything
 
Thanks for replying but still a bit lost here. This is the proposed setup:

Open my dbf (or load it when the form opens)
Do form myform
On this form is an edit box with the appropriate field taken from a memo field from my dbf
Also on the form is a command button for spell checking
When this is clicked it runs through the source code above

Please excuse my misunderstanding of this particular thread, but its the first time I've tried something a bit complex as this.
Thanks for your efforts in anticipation
KB (Thoughts to those in New York)
When everything else is said and done, there will be nothing else to say or do! God Bless everyone....
 
the way I understand it is that you want to disable the word spell check correction window when you have it spell check the editbox contents. is this correct? Attitude is Everything
 
Sorry Danceman, perhaps I should rewind and start again!

The code supplied from Microsoft (Q271819 - HOWTO Spell Check in Visual FoxPro with Microsoft Word) is really good and works well. My task here is to assign the contents of a memo field in my DBF and use the code to spell check it. I notice from the line within the code =

THIS.edtTextToCheck.VALUE = &quot;Thhis text has mistakees in it. We will seend &quot; + ;
&quot;it to Word and have it cheked.&quot;

Perhaps this line could be amended to accept a variable?

Hope I'm on the right track and sorry for any confusion this may be causing

KB When everything else is said and done, there will be nothing else to say or do! God Bless everyone....
 
keepingbusy

PROCEDURE Init
THIS.edtTextToCheck.VALUE = &quot;Thhis text has mistakees in it. We will seend &quot; + ;
&quot;it to Word and have it cheked.&quot;
ENDPROC


Perhaps this line could be amended to accept a variable?


I think it should be remove completely, I believe is only for the example only, so the example would have some text to deal with. I would add only line below to link the editbox your your memo field.

ADD OBJECT edttexttocheck AS editbox WITH ;
Height = 163, ;
Left = 23, ;
Top = 21, ;
Width = 332, ;
Name = &quot;edtTextToCheck&quot;;
ControlSource =&quot;myTable.myMemoField&quot;


Mike Gagnon
 
if thats what you want then what mgognon said is correct
assign the ControlSource =&quot;myTable.myMemoField&quot; of the edit box to memo filed. then in the program where the text is assigned

.Content.TEXT = ALLTRIM(THISFORM.edtTextToCheck.VALUE)
change to
.Content.TEXT = ALLTRIM(THISFORM.editboxofmemofiled.VALUE)


Attitude is Everything
 
Danceman
I'm sorry for the missleading naming of my function. My syntax is correct but you would have to name the function you put my code in it CheckSpelling:
Code:
FUNCTION CheckSpelling
PARAMETERS cText
LOCAL oWord,bRes
&& Create a Word Instance
oWord = createobject(&quot;Word.application&quot;)
&& Add Document
...

And so you can call this function and you get the corrected Text as return value.
In the Button click event you've only got to call this function:
Code:
PROCEDURE cmdcheckspelling.Click
  THISFORM.edtTextToCheck.VALUE = ;
    CheckSpelling(THISFORM.edtTextToCheck.VALUE)
ENDPROC
Just another question:
Does it make sense to look for word with api when you could use an error routine to catch this error?

Andreas
 
I just completed researching the ability to have word check spelling and to see if I could have word correct the spelling without the suggestion box appearing.

there are two methods of checkspelling in word. one is at the document level and the other is at the application. if you use the document level you will be presented with the suggestion dialog box. you can use checkspelling at the application level, but it will only tell you if there are errors, (return .t. ot .f.).
to make a long story short. you can not take your memo filed data to word, have it correct it without the suggestion dialog box, then retrive the text to put back in the memo.

if on the other hand If I misunderstood you and you are just trying to put your data in, correct the spelling with the dialog box, then retrive the corrected text then here is a simple example

oWord = createobject(&quot;Word.application&quot;)
oword.documents.add
oword.activedocument.content = thisform.edtibox.value
oword.activedocument.checkspelling
thisform.edtibox.value = oword.activedocument.range.text
oWord.quit(.F.)
Attitude is Everything
 
Many thanks for all the replies. I'll try this over the next few days and post a reply.

Once again, your efforts are appreciated
KB When everything else is said and done, there will be nothing else to say or do! God Bless everyone....
 
You could avoid displaying the correction dialog by doing the correction manually. If you assume that you leave words as is if there are no suggestions but you change words with the first available suggestion you could use the following code:
Code:
?CheckSpelling(&quot;thiss is wrongh spellld&quot;)
RETURN

FUNCTION CheckSpelling
  PARAMETER cText
  LOCAL oWord,bRes
  && Create a Word Instance
  oWord = createobject(&quot;Word.application&quot;)
  && Add Document
  WITH oWord.Documents.Add
    && populate Document with parametertext
    .content.text = cText
    && Spellcheck
    IF .SpellingErrors.Count > 0
      FOR EACH objSpellingError IN ; 
        oWord.ActiveDocument.SpellingErrors
        IF objSpellingError.GetSpellingSuggestions().Count > 0
          objSpellingError.text = objSpellingError.GetSpellingSuggestions().Item(1).name
        ELSE
          ' leave it miss spelled...
        ENDIF
      NEXT objSpellingError
      cText = .content.text
    ENDIF
  ENDWITH
  && ensure that word doesn't save the document
  oWord.quit(.F.)
  && return the corrected text
  RETURN cText
ENDFUNC
Andreas
 
Schweiger
you certainly know how to use the object browser! grate suggestion. when more then one correction suggested, the programmer would have to get all the suggestins and build a listbox of them for the user to select from. then put that back in the text. Now you must admit, if he only wants to get the corrected text back, this is not needed.

BUT, are we really sure of what keepingbusy wants? I think we have given keepingbusy enought to think about.

wish you well. Attitude is Everything
 
Hi All

Sorted at last! The scenario is that the user selects a record from a table (That's easy). When the record is shown, I have added a command button called Spell Check (strange, but true). Because the table is already at the record (Example RECNO()=20), when the command button is pressed (THISFORM.HIDE) I copied and pasted the code from the Microsoft suggestion and the line:

ControlSource =&quot;myTable.myMemoField&quot; (Which has been changed to the table and memo field names)

that mgagnon suggested. This works perfect with no problems whatsoever (Phew!). When the spellcheck process is complete, the form created by the source code is closed and on return to the original form (THISFORM.SHOW), it puts us back to where we want to be.

Well, we got there in the end and I have to say I am very grateful to everyone who contributed to this thread

Be safe wherever you are in the world
KB
When everything else is said and done, there will be nothing else to say or do! God Bless everyone....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top