In computing, a spell checker is a software program designed to verify the spelling of words. A spell checker helps a user to ensure correct spelling, while suggesting corrections for wrongly spelled words.
Spell checkers are either stand-alone applications capable of operating on a block of text, or as a feature of a larger application, such as a word processor, email client, electronic dictionary, or search engine.
In this article we create a spell checker using Visual Studio 2005 and Visual Basic.
Open Visual Studio 2005 by going to Start-> Programs-> Microsoft Visual Studio 2005-> Microsoft Visual Studio 2005 and start a new visual basic project by selecting File Menu-> New-> Project. In the dialog box that appears select Visual Basic Windows Application. Name this application Word Spelling Demo and create a user interface as shown in figure.
This user interface has one multiline TextBox control, two ListBox controls and two Buttons.
On the TextBox control the user can enter some text and spell check it by clicking on the SpellCheck Document Button.
The first ListBox control contains the list of the misspelled words of the TextBox control and the second ListBox contains the alternate spelling for the selected misspelled word in the first ListBox. To view the alternate spellings for a specific word, select the word in the left list by double-clicking it.
To replace all instances of the selected misspelled word with the selected alternative, click the Replace button. You can design your own interface to allow the user to select which and how many instances of the misspelled word in the original document will be replaced.
Since this application uses the Microsft Word’s capability of SpellCheck, therefore before beginning any coding include a Refernce of Microsoft Word(the version installed on your PC) into your project. To do this right click your project in the Solution Explorer Window and select Add Reference.
In the Add Reference dialog box go to the COM Tab and locate the Microsoft Word component and include it in your project.
Now we are all set to begin coding our application.
The program uses three public variables, which are declared as follows:
Public WordApp As Application
Public CorrectionsCollection As SpellingSuggestions
Public SpellCollection As ProofreadingErrors
The SpellCollection variable is a collection that contains all the misspelled words, and the
CorrectionsCollection variable is another collection that contains the suggested spellings for a specific word. The CorrectionsCollection variable’s contents are changed every time the user selects another misspelled word in Word’s Spelling Suggestions window.
When the SpellCheck Document button is clicked, the program creates a new document and
copies the TextBox control’s contents to the new document using the InsertAfter method of the
Range object, as follows:
WordApp.Documents.Add
Dim Drange As Word.Range
Drange = WordApp.ActiveDocument.Range
DRange.InsertAfter(Text1.Text)
Now comes the interesting part. The Visual Basic code calls the Range object’s SpellingErrors
method, which returns a collection of ProofreadingErrors objects. The result of the SpellingErrors
method is assigned to the object variable SpellCollection:
Dim SpellCollection As Word.ProofreadingErrors
Set SpellCollection = DRange.SpellingErrors
Now let us code the SpellCheck Document Button click event. Double click the SpellCheck Document Button in design mode.
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim DRange As Word.Range
Me.Text = “Starting Word ...”
WordApp.Documents.Add()
Me.Text = “Checking words...”
DRange = WordApp.ActiveDocument.Range
DRange.InsertAfter(TextBox1.Text)
Dim SpellCollection As Word.ProofreadingErrors
SpellCollection = DRange.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
newWord = SpellCollection.Item(iword).Text
If ListBox1.FindStringExact(newWord) < 0 Then
ListBox1.Items.Add(newWord)
End If
Next
End If
Me.Text = “Word spelling Demo”
End Sub
The document is checked in a single statement, which calls the SpellingErrors method of a Range object that contains the entire text. Then the program goes through every word in the SpellCollection collection and adds them to the ListBox1 control. Notice that the ListBox control with the misspelled words doesn’t contain duplicate entries. The code uses the control’s FindStringExact method to find out whether a word belongs to the list or not.
Put the following code in the ListBox1_SelectedIndexCahnged Event Handler:
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Dim CorrectionsCollection As Word.SpellingSuggestions
CorrectionsCollection = WordApp.GetSpellingSuggestions(ListBox1.Text)
ListBox2.Items.Clear()
If CorrectionsCollection.Count > 0 Then
Dim iWord As Integer
For iWord = 1 To CorrectionsCollection.Count
ListBox2.Items.Add(CorrectionsCollection.Item(iWord).Name)
Next
Else
ListBox2.Items.Add(“No suggestions!”)
End If
End Sub
Every time an entry in this ListBox is clicked, the code calls the WordApp object’s GetSpelling-
Suggestions method, passing the selected word as an argument. The GetSpellingSuggestions method returns another collection with the suggested words, which are placed in the second ListBox control on the Form. If this collection is empty (Word can’t suggest any alternatives), the string “No suggestions!” is displayed on the control.
You can also replace misspelled words in the document with one of the suggested alternatives by
clicking the Replace Word button. When you do, the application calls the Replace function to
replace all instances of the selected word with the selected alternative. After that, it removes the misspelled word from the list. The code behind the Replace Word button is
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
The Word Spelling Demo application can become the starting point for many custom Visual Basic applications that require spell-checking but don’t need powerful editing features. In some cases, you might want to customize spelling, although it’s not a very common situation. In a mail-aware application, for example, you can spell-check the text and exclude URLs and e-mail addresses. You would first scan the words returned by the SpellingErrors method to check which ones contained special characters and omit them.