Spell Checker Routine Using VB
User Rating: / 1
PoorBest 
Written by Girish Singh   







spell_check_vb_img3.pngIn 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.

spell_check_vb_img1.png



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.

spell_check_vb_img2.png


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.

spell_check_vb_img3.png







 

Quote this article on your site

  Comments (1)
 1 Comment 05 1427
Written by This e-mail address is being protected from spam bots, you need JavaScript enabled to view it , on 05-07-2008 19:27
Hielloo , indian river community college ft. pierce, preview of college football 2006 2007, guaranteed loan with bad credit, gay college chubby guys, glendale community college glendale, az, southwest airlines flight status, southwest airlines schedule time flight display, united airlines flight 93 passenger and crew phone calls, first appeared in the football college hall of fame, commercial loan interest rate, flight tickets to go to panama for cheap, flight simulator x addons, flight simulator 2004 add on, texas vet home loan, college girls party nude, airline flight tracker, desert flight jacket, florida minority business loan mobilization program, durham flight centre, flight attendant interviews, payday cash advance loan

Write Comment
  • Please keep the topic of messages relevant to the subject of the article.
  • Personal verbal attacks will be deleted.
  • Please don't use comments to plug your web site. Such material will be removed.
  • Just ensure to *Refresh* your browser for a new security code to be displayed prior to clicking on the 'Send' button.
  • Keep in mind that the above process only applies if you simply entered the wrong security code.
Name:
E-mail
Homepage
Title:
BBCode:Web AddressEmail AddressBold TextItalic TextUnderlined TextQuoteCodeOpen ListList ItemClose List
Comment:



Code:* Code
I wish to be contacted by email regarding additional comments


Powered by AkoComment Tweaked Special Edition v.1.4.6
AkoComment © Copyright 2004 by Arthur Konze - www.mamboportal.com. All right reserved

 
Next >




Privacy Notice | Advertising Info | Feedback | Contact Us | Partners

The information and views presented above are by the author. Mabaloo.com does not take any gurantee of accuracy.
The views expressed above are that of the author and in no way related to mabaloo.com

© 2007 @ mabaloo.com.