John V. Petersen
Need a spell checker in your Visual FoxPro applications? Chances are you and your clients already have an excellent tool for checking spelling -- Microsoft Word 97. The process of incorporating Word's spell checker into your application is very simple, and this month, John will show -- step by step -- how to accomplish this task.
Spelling and grammar checking in Word doesn't involve any complex operations. In fact, the entire operation can be accomplished with one method call. Depending on specific settings in Word, spelling and grammar can be checked together, or spelling can be checked alone. Word, like VFP, has an Options dialog box, and this dialog box has an exposed object interface. Therefore, any required environment setting can be handled with VBA code.
The Options objectWhen it comes to setting environmental options in Word, the Options Interface must be used. The first step, however, is to create an instance of Word:
oWord = CreateObject("word.application")
To illustrate changing a setting, assume that I want to enable checking grammar at the same time spell checking occurs. With an object reference to Word, the following code will do the trick:
oWord.Options.CheckGrammarWithSpelling = .T.
As always, the Object Browser in Visual Basic can provide information regarding the PEMs of various objects. This is illustrated in Figure 1. (For more information on the Object Browser, please refer to my column, "Automation from a VFP Perspective," in the May 1998 issue of FoxTalk.)
Figure 1: The Options object provides an interface to environment settings in Word
Another property of the Options object that's relevant to spelling and grammar checking is the IgnoreUppercase Property. If this property is set to .T., then any words that are comprised of upper-case letters are ignored. The following code sets this property:
oWord.Options.IgnoreUppercase = .T.
The CustomDictionaries collection
How does Word know whether a word is spelled correctly? It has a dictionary, of course! In fact, Word can have multiple dictionaries, and you can choose whether to use one, some, or all of them when checking spelling.
The CustomDictionaries Collection is comprised of one or more dictionary objects. The CustomDictionaries Collection is a member of the Word Application Object and can be accessed as follows:
For Each oDictionary in oWord.CustomDictionaries
?oDictionary.Path
?oDictionary.Name
Next oDictionary
The available dictionaries in your installation of Word can be viewed in the Spelling and Grammar tab of the Word Options dialog box. In this dialog box, you can modify the contents of dictionaries, as well as add and remove dictionary definitions.
An important property of the CustomDictionaries collection is the ActiveCustomDictionary. In the absence of specifying a dictionary to perform spell checking, the dictionary referenced by ActiveCustomDictionary will be used. The following code displays the Path and Name properties of the ActiveCustomDictionary object:
With oWord.CustomDictionaries.ActiveCustomDictionary
?.Path
?.Name
EndWith
Note that the ActiveCustomDictionary isn't a read-only property. Assume I want to make the second dictionary in the CustomDictionaries collection the active dictionary. The following code would do the trick:
oWord.CustomDictionaries.ActiveCustomDictionary = ;
oWord.CustomDictionaries.Item(2)
The CheckSpelling() and CheckGrammar() methods
Now that I've covered the properties you're most likely going to be interested in, it's time to discuss where the action occurs: the methods. As previously mentioned, spelling can be checked along with grammar, or spelling can be checked separately. If the CheckGrammarWithSpelling property of the Options object is set to .T., when the CheckGrammar() method is invoked, spelling will also be checked. However, regardless of the CheckGrammarWithSpelling property setting, CheckSpelling will only invoke spell checking.
There are two methods of spelling and grammar checking, which I'll refer to as active and passive:
To illustrate, if I want to check for spelling errors in a string of text, I could make the following method call:
Pass = oWord.CheckSpelling("thsi")
This method assumes that the needed settings have been made in the Options dialog box. To be sure, the method call could be modified as follows:
dictionary = oword.customdictionaries(1).path + ;
"\"+oword.customdictionaries(1).name
Pass = oword.checkspelling("thsi",dictionary,.T.)
Checking grammar is very simple as well. The CheckGrammar() method has one optional argument, a text string. So, to check the grammar of a sentence:
Pass = oWord.checkgrammar("This is pretty cool.")
While passive spelling and grammar checking is nice, I'm not sure how useful it is. Spelling and grammar checking seem to be interactive processes by nature. The following section illustrates how active checking can be incorporated into your applications.
Putting it all together in VFPFigure 2 illustrates a VFP form that integrates the spelling and grammar checking capabilities of Microsoft Word.
Figure 2: A form launches Microsoft Word, which, in turn, spell and grammar checks the text in the EditBox control.
Here's how this works. When the VFP form loads, an instance of Word is created, and the contents of the CustomDictionaries collections is added to the ListBox control. In addition, the CheckGrammarWithSpelling and IgnoreUpperCase property settings are used to provide initial values for the two check boxes. The following code is in the Init() of the form:
This.oWord = CreateObject("word.application")
With This.lstDictionaries
For Each oDictionary In This.oWord.CustomDictionaries
Dictionaryitem = oDictionary.Path + "\" ;
+ oDictionary.Name
.AddItem(Dictionaryitem)
Next oDictionary
.listitemid = 1
EndWith
This.chkGrammar.Value = ;
This.oWord.Options.CheckGrammarWithSpelling
This.chkIgnoreCase.Value = ;
This.oWord.Options.IgnoreUpperCase
The processing occurs in the Click() of the CommandButton. Here's the code:
With Thisform.oWord
.Documents.add
.Selection.TypeText(ThisForm.edtText.Value)
.Visible = .T.
.Options.CheckGrammarWithSpelling = ;
This.Parent.chkGrammar.Value
.Options.IgnoreUpperCase = ;
This.Parent.chkIgnoreCase.Value
.CustomDictionaries.ActiveCustomDictionary = ;
.CustomDictionaries( ;
ThisForm.lstDictionaries.ListItemID)
If This.Parent.chkGrammar.Value
.Activedocument.CheckGrammar
Else
.Activedocument.CheckSpelling
Endif
.Selection.WholeStory
ThisForm.edtText.Value = .Selection.Text
.ActiveDocument.Close(0)
.Visible = .F.
EndWith
Figure 3 illustrates the VFP form in action.
Figure 3: The CheckSpelling and CheckGrammar methids in Word are synchronous processes. Once they're complete, control is returned to the automation client – VFP, in this case.
ConclusionWhy bother with third-party products when a solution already exists? Yes, this solution is predicated on having Microsoft Word 97, but this is an assumption you might be able to make. If your clients use Word, then by taking this approach, they already know how to use part of your application; as I've shown, the process of integrating Word and VFP is very simple.
Download sample code for this article here.
John V. Petersen, MBA, is vice president of IDT Marketing Systems and Services, a Philadelphia-based marketing database consulting firm. John has presented at numerous developer conferences, including DevCon 97, Tech-Ed, and the Southwest Visual FoxPro Conference. John is a Microsoft Most Valuable Professional (MVP) and a co-author of Developing Visual FoxPro 5.0 Enterprise Applications and Hands-On Visual Basic 5 -- Web Development, both from Prima Publishing. j_petersen@idtmarketing.com.