Contents
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.
Top 
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()
.Activate()
If blnSpellOnly Then
.CheckSpelling()
Else
.CheckGrammar()
End If
After the spell/grammar checker has run and any changes have been made by the user, transfer the contents back to TextBox1:
.Content.Copy()
iData = Clipboard.GetDataObject
If iData.GetDataPresent(DataFormats.Text) Then
TextBox1.Text = CType(iData.GetData(DataFormats.Text), _
String)
End If
.Saved = True
.Close()
End With
It's now time to quit Word and send back a message to the user; in the same way that Word does when the spell/grammar checker is complete:
objWord.Quit()
MessageBox.Show("The spelling check is complete.", _
"Spell Checker", MessageBoxButtons.OK, _
MessageBoxIcon.Information)
The last thing to do in this routine is to add in the error handling Catch statements. Two Catch blocks are defined; one to determine if Word is installed, as it is required; and the other to handle gracefully any other errors by displaying an error message:
Catch COMExcep As COMException
MessageBox.Show( _
"Microsoft Word must be installed for Spell/Grammar Check " _
& "to run.", "Spell Checker")
Catch Excep As Exception
MessageBox.Show("An error has occurred.", "Spell Checker")
End Try
End Sub
Finally, the code to call the above routine must be added to the grammar and spell checker buttons:
Private Sub btnSpellCheck_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnSpellCheck.Click
SpellOrGrammarCheck(True)
End Sub
Private Sub btnGrammarCheck_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnGrammarCheck.Click
SpellOrGrammarCheck(False)
End Sub
Top 
Conclusion
This article has described how to use the spelling and grammar checking facilities of Microsoft Word in a stand-alone Windows application. Using object oriented features and COM Interop, the addition of these features to your own applications can add that professional touch while utilizing software that is widely available and already installed in many cases.
One thing you may want to consider when adding this functionality to your own applications would be to disable the buttons or menu items for spelling/grammar options if Word isn't detected on the client computer. This would give the polished appearance of a professional application.
The attached to the article solution is based on Word 2002 and references Office/Word v10 Object Library. It may not be the version you’ve got (for instance - Office 2000, Version 9) – please expect this and remove Office/Word v10 Object Library references from the project and replace them with the appropriate one for your version.
The following is a complete listing of all code presented in this article:
Imports System.Runtime.InteropServices
Public Class Form1
Inherits System.Windows.Forms.Form
' Invokes either the spell or grammar checker.
Private Sub SpellOrGrammarCheck(ByVal blnSpellOnly As Boolean)
Try
' Create Word and temporary document objects.
Dim objWord As Object
Dim objTempDoc As Object
' Declare an IDataObject to hold the data returned from the
' clipboard.
Dim iData As IDataObject
' If there is no data to spell check, then exit sub here.
If TextBox1.Text = "" Then
Exit Sub
End If
objWord = New Word.Application()
objTempDoc = objWord.Documents.Add
objWord.Visible = False
' Position Word off the screen...this keeps Word invisible
' throughout.
objWord.WindowState = 0
objWord.Top = -3000
' Copy the contents of the textbox to the clipboard
Clipboard.SetDataObject(TextBox1.Text)
' With the temporary document, perform either a spell check or a
' complete
' grammar check, based on user selection.
With objTempDoc
.Content.Paste()
.Activate()
If blnSpellOnly Then
.CheckSpelling()
Else
.CheckGrammar()
End If
' After user has made changes, use the clipboard to
' transfer the contents back to the text box
.Content.Copy()
iData = Clipboard.GetDataObject
If iData.GetDataPresent(DataFormats.Text) Then
TextBox1.Text = CType(iData.GetData(DataFormats.Text), _
String)
End If
.Saved = True
.Close()
End With
objWord.Quit()
MessageBox.Show("The spelling check is complete.", _
"Spell Checker", MessageBoxButtons.OK, _
MessageBoxIcon.Information)
' Microsoft Word must be installed.
Catch COMExcep As COMException
MessageBox.Show( _
"Microsoft Word must be installed for Spell/Grammar Check " _
& "to run.", "Spell Checker")
Catch Excep As Exception
MessageBox.Show("An error has occured.", "Spell Checker")
End Try
End Sub
Private Sub btnSpellCheck_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnSpellCheck.Click
SpellOrGrammarCheck(True)
End Sub
Private Sub btnGrammarCheck_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnGrammarCheck.Click
SpellOrGrammarCheck(False)
End Sub
End Class
Top 