Article source code: spellcheck.zip
Introduction
Many applications can be enhanced by including spelling and grammar checking capabilities. Users will greatly appreciate these features and in addition, applications will appear more professional as they mimic popular programs like MS Word. This article will demonstrate how to add these functions to a Visual Basic.NET Windows Form project. It's quite easy to do, and only requires that the client application have Microsoft Word installed. The code in this article has been successfully tested with Microsoft Word 97, 2000, XP and 2003 Beta 2.
Create a new Windows Application project in Visual Studio.NET. The Form will contain two buttons and one TextBox control. Add controls to the Form with the following settings:
| Type | Name | Text |
| Button | btnSpellCheck | Spell Check |
| Button | btnGrammarCheck | Grammar Check |
| Textbox | Textbox1 | |
|
The Object Libraries for Microsoft Word are not written natively in .NET. A COM wrapper is required to import the required spelling and grammar components. Right click on References under the Windows Application in the Solution Explorer. Click on Add Reference... and then click on the COM tab in the Add Reference dialog box. Scroll down the list until you find Microsoft Word Object Library. There will be a version number associated with it, in my case (Word 2002), I see version 10.0. Select this object by clicking on the Select button and then on the OK button.
Code
It is now time to get down to coding. The first thing to do is add the following Imports statement:
Imports System.Runtime.InteropServices
This will provide for the necessary error handling further on in the code.
The next thing to do is create a subroutine for calling the spell checking component. This subroutine is called from the click events of the btnSpellCheck and btnGrammarCheck. One Boolean parameter is passed to determine if the spell checker or grammar checker is invoked. Keep in mind that the grammar checker also checks spelling, whereas the spelling checker only checks spelling and not grammar:
Private Sub SpellOrGrammarCheck(ByVal blnSpellOnly As Boolean)
The next thing we'll do is define some object variables to hold the instance of a Word application, Word document and an IDataObject to hold data returned from the clipboard. All of this occurs with a Try...Catch block:
Try
Dim objWord As Object
Dim objTempDoc As Object
Dim iData As IDataObject
The data to spell check is contained in TextBox1. If there is nothing contained in the text, then it is fair to skip the rest of this subroutine and just exit:
If TextBox1.Text = "" Then
Exit Sub
End If
The next thing to do is instantiate the Word application, add a temporary document to it and position Word so it's not visible:
objWord = New Word.Application()
objTempDoc = objWord.Documents.Add
objWord.Visible = False
objWord.WindowState = 0
objWord.Top = -3000
Copy the text contents of TextBox1 to the clipboard:
Clipboard.SetDataObject(TextBox1.Text)
Now that we have the data in the clipboard we can copy it to our temporary document and activate the spell/grammar check on the Word document:
With objTempDoc
.Content.Paste()
.