Creating a Custom Spell-Checker with Word 2003 and Visual Basic .NET
Summary: You can tie into the objects and methods of Microsoft Office Word 2003 to create powerful applications. This article demonstrates using the Word object model in conjunction with Microsoft Visual Basic .NET to create a simple spell-checker whose functionality you can add to your own applications. (6 printed pages)
Frank Rice, Microsoft Corporation
Applies to: Microsoft Office Word 2003, Microsoft Visual Basic .NET
Tapping Into the Spell-Checking Functionality of Word
The Spell-Checker Application
Microsoft Office Word 2003 comes with a number of reference resources. These include a dictionary, a grammar guide, a thesaurus, and a spell-checker. These reference tools enable you to apply spelling and grammatical changes automatically, making it easy to create accurate and credible documents.
Additionally, while you can use the spell-checker intrinsically from Word, this functionality is also exposed to other applications using the object model. This means that you can also use these features in your own applications. This article looks at the objects, methods and techniques used to implement a spell-checker programmatically in a Microsoft Visual Basic .NET application.
Tapping Into the Spell-Checking Functionality of Word
Before jumping into creating the spell-checker application, let's examine two objects: the ProofreadingErrors collection and the SpellingSuggestions collection. The ProofreadingErrors collection is a property of the Range object and contains any misspelled words in the Range object. The Range object contains text anywhere from a word, a sentence; a paragraph; or an entire document. You can populate the ProofreadingErrors collection by calling the Range object's SpellingErrors method as in the following code segment:
Dim SpellCollection As ProofreadingErrors Set SpellCollection = rngRange.SpellingErrors
After the declaration, the second line populates the
SpellCollection variable with the list of misspelled words. You can then use a For Each...Next statement to read each word from the collection.
Besides compiling a list of spelling errors, you can also use the Word objects to suggest alternate spellings. To retrieve a list of alternate words, you call the GetSpellingSuggestions method of the Application object, passing the misspelled word as an argument. The results returned by the GetSpellingSuggestions method are stored in another collection; the SpellingSuggestions collection as in the following code segment:
Dim ListOfAlternateWords As SpellingSuggestions Set ListOfAlternateWords = AppWord.GetSpellingSuggestions("baskitball")
In this segment, the second line retrieves a list of alternatives for the argument baskitball. You can then loop through the list of alternatives in the ListOfAlternateWords collection by using a For Each...Next loop.
The Spell-Checker Application
To demonstrate creating and using a spell-checker with object and methods from Word, we create an application in Visual Basic .NET that checks a text file for spelling errors.
|The code in this application is available as a downloadable file from the Microsoft Download Center.|
The application consists of two TextBox controls, two ListBox controls, and four CommandButton controls located on a Microsoft Windows Form (see Figure 1).
Figure 1. Spell-checker user interface
The following table outlines the Command button control assignments:
Table 1. List of button name assignments
When you click Button1, the application checks the spelling of the text in Textbox1. The application then requests a list of misspelled words. The code uses that list to populate Listbox1. When you click one of the misspelled words, a list of alternative words appears in Listbox2. When you then click one of the alternative words and then click Button2, the alternative word replaces all instances of the misspelled word in the text box.
Listing 1 contains the procedure attached to Button1 that spell-checks the text and adds the misspelled words in the SpellCollection collection to Listbox1:
Listing 1. Spell-Check Procedure
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim rngRange As Word.Range Me.Text = "Starting Word ..." WordApp.Documents.Add() Me.Text = "Checking text..." rngRange = WordApp.ActiveDocument.Range rngRange.InsertAfter(TextBox1.Text) Dim SpellCollection As Word.ProofreadingErrors SpellCollection = rngRange.SpellingErrors If SpellCollection.Count > 0 Then ListBox1.Items.Clear() ListBox2.Items.Clear() Dim iword As Integer Dim newWord As String For iword = 1 To SpellCollection.Count If SpellCollection.Item(iword).Text <> TextBox2.Text Then newWord = SpellCollection.Item(iword).Text If ListBox1.FindStringExact(newWord) < 0 Then ListBox1.Items.Add(newWord) End If End If Next End If Me.Text = "Word Spelling-Checker" End Sub
The text is checked for spelling errors with the following statements; which call the SpellingErrors method of the Range object. The Range object contains all of the text from Textbox1:
Dim SpellCollection As Word.ProofreadingErrors SpellCollection = rngRange.SpellingErrors
The code checks each word in the SpellCollection collection and adds the misspelled words to Listbox1. Note that the list of misspelled words in the list box does not contain any duplicate words. This occurs because the code uses the list box's FindStringExact method to check for duplicate words.
The next procedure (see Listing 2) is called when you click a word in Listbox1. The code calls the WordApp object's GetSpellingSuggestions method, using the selected word as an argument. The GetSpellingSuggestions method returns a collection of alternate words that are used to populate Listbox2. If the collection is empty, the string No suggestions. is displayed.
Listing 2. Alternate Word List Procedure
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged Dim ListOfAlternateWords As Word.SpellingSuggestions ListOfAlternateWords = _ WordApp.GetSpellingSuggestions(ListBox1.Text) ListBox2.Items.Clear() If ListOfAlternateWords.Count > 0 Then Dim iWord As Integer For iWord = 1 To ListOfAlternateWords.Count ListBox2.Items.Add(ListOfAlternateWords.Item(iWord).Name) Next Else ListBox2.Items.Add("No suggestions.") End If End Sub
The procedure contained in Listing 3 demonstrates how to replace the misspelled words in the text in Textbox1. You execute the procedure by clicking the Replace Word button. When you do, the application calls the Replace method that replaces all occurrences of the word that appear in the text with the alternate word.
Listing 3. Replace Misspelled Word Procedure
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click If ListBox1.SelectedIndex >= 0 And ListBox2.SelectedIndex >= 0 Then TextBox1.Text = Replace(TextBox1.Text, ListBox1.SelectedItem, ListBox2.SelectedItem) ListBox1.Items.Remove(ListBox1.SelectedIndex) ListBox2.Items.Clear() End If End Sub
Next, the following code clears the two list boxes. The code works when you click Button3 and executes the Clear method of the list boxes.
Listing 4. Procedure to Clear the List Boxes
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click ListBox1.Items.Clear() ListBox2.Items.Clear() End Sub
And finally, use this next example to attach the first subroutine in Listing 5 to Button4 and close the application by closing Form1. As Form1 closes, it triggers the Closing event of the form (the second subroutine), which then executes the Application object's Quit method, specifying not to save the changes.
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click Me.Close() End Sub Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing WordApp.Quit(False) End Sub
In this article, you saw one way to use the Word object model from within a Visual Basic .NET application to create a simple yet useful application. Taking the spell-checker application as a starting point, you can extend your own applications or add additional functionality to this particular application. For example, you might want to exclude certain words, such as foreign words or e-mail addresses, from spell-checking.
In addition, you saw in this article that using the functionality of Office programs isn't complicated. Once you are familiar with the basic object properties and methods of a specific Office program, you can capture the power of the program by manipulating just a few properties and methods and create powerful applications with little effort on your part.