×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

VERY SIMPLE use of MS WORD Spell checker

VERY SIMPLE use of MS WORD Spell checker

VERY SIMPLE use of MS WORD Spell checker

(OP)
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.

RE: VERY SIMPLE use of MS WORD Spell checker

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

RE: VERY SIMPLE use of MS WORD Spell checker

(OP)
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.

RE: VERY SIMPLE use of MS WORD Spell checker

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?

RE: VERY SIMPLE use of MS WORD Spell checker

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

RE: VERY SIMPLE use of MS WORD Spell checker

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

RE: VERY SIMPLE use of MS WORD Spell checker

(OP)
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.

RE: VERY SIMPLE use of MS WORD Spell checker

Interesting. You can also pass the IgnoreUppercase parameter of the CheckSpelling method. That way, you don't have to instruct your customers to manually alter Proofing options. Check here for the CheckSpelling parameters: https://docs.microsoft.com/en-us/office/vba/api/wo...

RE: VERY SIMPLE use of MS WORD Spell checker

(OP)
Vernpace
Yes, I understand how to do this....Thanks!

RE: VERY SIMPLE use of MS WORD Spell checker

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

RE: VERY SIMPLE use of MS WORD Spell checker

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

RE: VERY SIMPLE use of MS WORD Spell checker

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

RE: VERY SIMPLE use of MS WORD Spell checker

(OP)
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.

RE: VERY SIMPLE use of MS WORD Spell checker

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

RE: VERY SIMPLE use of MS WORD Spell checker

Hi Tamar,

Thanks for posting. I will check this tomorrow hopefully.

Regards, Gerrit

RE: VERY SIMPLE use of MS WORD Spell checker

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

RE: VERY SIMPLE use of MS WORD Spell checker

Gerrit,

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



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 

RE: VERY SIMPLE use of MS WORD Spell checker

Quote:


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

RE: VERY SIMPLE use of MS WORD Spell checker

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

RE: VERY SIMPLE use of MS WORD Spell checker

Mike,

The problem with your approach is it's dependency on Word's UI. This can be problematic for applications that want to fully integrate spell-checking. While my approach is much more complex, it is fully integrated.

Sometimes, we have to go the extra mile. Sometimes, we need to enhance our skills. And all these "Great Post" marks on this forum is BS.

RE: VERY SIMPLE use of MS WORD Spell checker

Quote (vernpace)

And all these "Great Post" marks on this forum is BS.

Why?

RE: VERY SIMPLE use of MS WORD Spell checker

Because it reminds me of Facebook's ThumbsUp - and I can't stand FaceBook...

RE: VERY SIMPLE use of MS WORD Spell checker

Vernspace,

The point of Tek Tips' "Great post" feature is that it alerts forum members to problems that have either been resolved or which have attracted helpful replies. When someone browses the list of threads - perhaps to look for threads similar to their own problems, or simply to find questions they might be able to answer - the presence of a red start can point them in the right direction. Similarly, if you are browsing within a thread, the red stars can tell you at a glance which replies have helped to solve the problem.

I would agree that the system is sometimes mis-used and doesn't always achieve its aims. But to condemn it simply because it reminds you of a feature you dislike in Facebook seems a little over the top.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads

RE: VERY SIMPLE use of MS WORD Spell checker

Quote (Mike Lewis)

The point of Tek Tips' "Great post" feature is that it alerts forum members to problems that have either been resolved or which have attracted helpful replies.

With the recent shenanigans, I am sure the 'Great Post' system has been used (abused?) to support the content of the person posting rather than the problem has been resolved. I am guilty of this action.

Regards,

David

Recreational Developer and End User of VFP for my personal use in 'Amateur Radio' and 'British Railways' related Applications.

RE: VERY SIMPLE use of MS WORD Spell checker

Thank you JohnWMac and Mike Lewis. You have encouraged me to include spell checking at various points in an application.
I have gone for Mike’s approach of using Microsoft Word, and the slightly modified code for my cmdSpellcheck.click() is :

CODE -->

LOCAL loWord, loDoc, llWasVisible
 
loWord = CREATEOBJECT("Word.Application")
IF !(TYPE("loword") = "O")
   MESSAGEBOX("Cannot perform the spell check." + CHR(10) + CHR(10) + ;
   "Unable to open Microsoft Word", 48)
   RETURN
   ENDIF 		

* Go ahead and do the spell check 
WITH loWord
   loDoc = .Documents.add        && Open a new doc within Word
	
   * Copy the text to the new doc
   loDoc.Range(0,0).InsertAfter(thisform.frmcust.page2.edtnotes.Value)
   
   * Make it visible
   llWasVisible = .visible		&& save the visible state
   .visible = .T.
   .WindowState = 0			&& normal window
   .Activate
   loDoc.CheckSpelling     && Call the spell checker

   * Copy the text back 
   thisform.frmcust.page2.edtNotes.Value = loDoc.content.text
   loDoc.close(0)          && Close the Word doc (without prompting)

   * 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 


This does indeed invoke the (Office 365) word spell checker, and a task is created with a flashing W showing in the Windows TaskBar. But the original application – particularly if it is running full-screen - is still the only one showing on the screen.

What code should I include to make the Word application - which is telling me interesting thing about the text I am validating - come to the forefront until I send it away and revert to my own application?

RE: VERY SIMPLE use of MS WORD Spell checker

(OP)
AndrewMozley I believe all you need to do is turn on the "visible" property which Mike's code actually does. Again, in my case, where I just want to spellcheck a single word, I want WORD to be as quiet as possible. BTW, and I didn't say this previously, my interest in spellchecking is because I am writing a program to simulate the game WORDLE (https://www.nytimes.com/games/wordle/index.html) which is posted daily in the New York Times. (Yes this is a trivial project and it is just to entertain myself, but it's better than watching TV). The advantage of my program is that you can play all day long given the ability for another person to enter a word after which you take over and try to resolve the puzzle. Obviously, the spellchecker prohibits both the target word and any of your guesses from being non-words.

RE: VERY SIMPLE use of MS WORD Spell checker

Andrew,

I'm pleased that you found my code useful. I see that you have modified it to make it more self-contained, which is good.

My version uses a separate method to open Word. This goes beyond a simple CREATEOBJECT(), in that it: (i) it only needs to launch Word once (within a given form); and (ii) if Word is already instantiated, it uses the existing object reference. However, none of this is essential; I'm not suggesting you should change your version.

I should also have mentioned that my version relies on the presence of a custom property, oWord, to store the object reference.

If anyone is interested, here is my code for instantiating Word:

CODE -->

* Try to instantiate Word. If successful, return .T. 

LOCAL loWord, llError, llJunk

IF NOT ISNULL(thisform.oWord)
	* Word has been instantiated ...
	TRY
		llJunk = thisform.oWord.Visible
	CATCH
		* ... but an attempt to access its PEMs has failed;
		* this indicates that the user closed Word interactively
		* since it was last instantiated; the solution is to
		* clear out the object reference so that we can instantiate
		* it again.
		thisform.oWord = NULL
	ENDTRY
ENDIF 
	
IF ISNULL(thisform.oWord)
	* Word not yet instantiated (in this form). So first try to get
	* a reference to an existing Word session
	
	TRY
		llError = .f.
		loWord = GETOBJECT(,"word.application")
	CATCH
		llError = .t.
	ENDTRY
	
	IF llError
		* That didn't work, so try for a reference to an existing Word session
		TRY
			llError = .f.
			loWord = CREATEOBJECT("word.application")
		CATCH
			llError = .t.
		ENDTRY
		
		IF llError
			* That didn't work either, so jump out
			RETURN .F.
		ENDIF 
		
	ENDIF 

	* Successfully opened Word. Store the object ref.
	thisform.oWord = loWord
	
	RETURN .t.
	
ENDIF 

You would store this in a method of the form (and create the custom oWord property for the same form).

By the way, the above code is not specific to spell checking. It simply instantiates Word, which you can then use for whatever you want.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads

RE: VERY SIMPLE use of MS WORD Spell checker

Thank you John. The Word window certainly can be visible, showing possible spelling corrections. But if my application is running full screen, and is in the foreground, then the Word window cannot be seen.

I would like to bring the Word screen to the foreground, so that it is visible (until the user sends it away).

Is there a VFP instruction which I can put into my application to make this happen?

RE: VERY SIMPLE use of MS WORD Spell checker

John,

You say you are using the single-word version of Word's spell checker to write a Wordle program. Could I suggest a better approach.

Rather than relying on Microsoft's dictionary, which might contain all kinds of entries that might not be Wordle-compliant (personal names, acronyms, etc), you can download your own word list and customise it to your needs.

I have recently been using a 194,000-word dictionary, which I downloaded from http://www.gwicks.net/dictionaries.htm. In fact, I found that this contained too many highly obscure words for my needs, so I switched to a smaller 65,000-word version, which I got from the same site.

Once you have downloaded the dictionary (which is a simple text file), you can create a sub-set containing only the five-letter words. You could import this into a DBF, then index it and search it with SEEK or LOCATE or whatever. You would then have a very much simpler and faster method of verifying a word, without the overhead of calling Microsoft Word.

And once you have your own dictionary, the door is open to creating all kinds of word games and puzzles. I have been using it to create a Trackword puzzle, a palindrome generator and an anagram solver. If you are interested, look over some of the recent threads in the Puzzles for Programmers forum, starting with thread1551-1811770: English dictionary fun.

Good luck with the project. As you say, it's better than watching television.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads

RE: VERY SIMPLE use of MS WORD Spell checker

Windows has a (little-documented) built-in spelling interface, does not need Word ...

Unfortunately it isn't pure ActiveX, and requires use of a tlb and robust early binding (Createobject can't be used). Foxpro isn't my area of expertise, so don't know if it can work there - but it is relatively easy in VBA/VB6. I'd be happy to provide my demo VB6 code if there is anyone with the expertise necessary to translate it to FoxPro

RE: VERY SIMPLE use of MS WORD Spell checker

Strongm,

that would be nice, I don't see the need to translate this into VFP, as VB can make it a COM control and we could use that in VFP.
Or is it VB.NET? Then there's the Interop Forms Toolkit,, which aimed at VB6 developers to help migrating to .NET but also is usable for interop with VFP.

Chriss

RE: VERY SIMPLE use of MS WORD Spell checker

(OP)
Mike Lewis
Thank you Mike for your information on another way to deal with spellchecking. I will play with this and provide feedback. I am not enamored with the call to WORD and although it works for me since I have MS Office installed, how about providing a copy of the program to a friend/customer who may not have OFFICE...I'm pretty sure that's part of what you were suggesting with your "Foxpro-only" solution.

RE: VERY SIMPLE use of MS WORD Spell checker

Quote (Mike Lewis)

a 194,000-word dictionary, which I downloaded from http://www.gwicks.net/dictionaries.htm

Mike,

I used that dictionary in creating a VFP app to help me with difficult (for me anyway) crossword puzzles. For example if I'm looking for a six-letter word with first and third letters W & G, I type "W,G,,," in the textbox and the returns are listed.

You are right about the dictionary containing unrecognizable words.

Steve

RE: VERY SIMPLE use of MS WORD Spell checker

>Or is it VB.NET

No, it is classic VB6.

But it is only a basic demonstration of the technique, not in any shape to be turned into a robust COM control (and I'm afraid I don't have the urge to do that myself currently ...).



RE: VERY SIMPLE use of MS WORD Spell checker

Steve,

Good to hear that I am not the only VFP person who uses that dictionary, and I like the idea of your crossword solver. You probably know that there are dozens of Androiod and iPhone apps that do the same thing (I use one called Crossword King), but it is much more satisfying to write your own.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads

RE: VERY SIMPLE use of MS WORD Spell checker

Andrew,

I'll try to answer your question about how to bring Word to the foreground.

When I run your code as it stands, I see Word appearing on the taskbar, but it is behind VFP's own window, which is not what you want. So I put _screen.WindowState= 1 just before loDoc.CheckSpelling. So now the VFP program is minimised and Word becomes visible. That seems to have solved the problem, but I don't really understand why it was happening. I don't rmember it being an issue before.

I also put _screen.WindowState= 2 to bring the VFP window back to maximised once the spell check is finished.

I noticed a couple of other points about your (our) code:

- The messagebox at the end has as its second parameter something called gcShortTitle. This is something that is specific to my applications (and which you are probably using as well), so it should be removed to prevent an error.

- In fact, I would argue that the messagebox shouldn't be there at all. It should be obvious to the user that the spell check has finished. But I can see that you would probably find it useful when testing the program.

- It is important to close Word when you have finished the spell check, otherwise a new instance will be loaded every time you run it. So add .Quit towards the end of the WITH / ENDWITH loop.

In my own version, I handled the closing of Word in the form's Destroy, as follows:

CODE -->

* Close the Word session that was used for spell checking (if any)
IF NOT ISNULL(thisform.oWord)
  * We did open Word in this form
   IF thisform.oWord.documents.count = 0
      * and there are no other documents open
      thisform.oWord.quit
  ENDIF
ENDIF 


As you can see, this will only close Word if it was opened in the form itself (rather than by another form or another application or by the user interactively), and only if no other documents are open. But you don't really need to do this in your version, as you are opening and closing Word each time you run the spell check.

I hope this all makes sense.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads

RE: VERY SIMPLE use of MS WORD Spell checker

For those interested in getting a list of spelling suggestions for a misspelled word without resorting to using Word's UI. Note: you will need to modify as this returns XML required for our contextmenu control.

CODE -->

LPARAMETERS tcSuggestedWordsXML AS String
LOCAL lcWord, lcWordKey, lcSuggestedWord, lcSuggestedWordXML, lcSuggestedWordsXML, loWord

STORE "" TO lcSuggestedWordXML, lcSuggestedWordsXML

lcWord = This.CallingObject.SelText
loWord = CREATEOBJECT("Word.Application")
loWord.Documents.Add
DOEVENTS

IF NOT loWord.CheckSpelling(lcWord)
   FOR i = 1 TO loWord.GetSpellingSuggestions(lcWord).Count
       lcWordKey = "WORD" + TRANSFORM(i)
       lcSuggestedWord = loWord.GetSpellingSuggestions(lcWord).Item(i).Name

TEXT TO lcSuggestedWordXML TEXTMERGE NOSHOW
    <MenuItem text="<<lcSuggestedWord>>" itemtype="256" itemstate="0" key="<<lcWordKey>>" tag=""/>
ENDTEXT

       lcSuggestedWordsXML = IIF(EMPTY(lcSuggestedWordsXML), lcSuggestedWordXML, lcSuggestedWordsXML + CRLF + lcSuggestedWordXML)

   ENDFOR
ELSE
   lcSuggestedWordsXML = "NOPROCESS"
ENDIF

loWord.Quit
RELEASE loWord
DOEVENTS

tcSuggestedWordsXML = lcSuggestedWordsXML 

RE: VERY SIMPLE use of MS WORD Spell checker

And here is how you can add a word to Word's custom dictionary:

CODE -->

LOCAL lcWord, loWord, liBytes

lcWord = This.CallingObject.SelText
loWord = CREATEOBJECT("Word.Application")
liBytes = AddToCustomDictionary(loWord, lcWord)

loWord.Quit
RELEASE loWord
DOEVENTS

*!*    Quality check
*!*    To be used with RTF spell-checker
FUNCTION AddToCustomDictionary(toWord AS Object, tcWord AS String) AS Integer
   LOCAL lcCustomDictionaryPath, lcWord, lcWordString, loCustomDictionary, liBytes

   loCustomDictionary = toWord.CustomDictionaries.ActiveCustomDictionary
   lcCustomDictionaryPath = ADDBS(loCustomDictionary.Path) + loCustomDictionary.Name

   IF NOT FILE(lcCustomDictionaryPath, 2)
      RETURN -1
   ENDIF

   lcWord = STRCONV(tcWord, 5)
   lcWordString = FILETOSTR(lcCustomDictionaryPath)
   
   IF lcWord $ lcWordString
      RETURN 0
   ENDIF

   lcWord = lcWord + CHR(13) + CHR(0) + CHR(10) + CHR(0)
   liBytes = STRTOFILE(lcWord, lcCustomDictionaryPath, 1)

   RETURN liBytes

ENDFUNC 

Just about ANYTHING can be done in VFP.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close