Working on a project I needed to be able to offer the user the ability to spell check the content they entered into a text editor. Not wanting to use a third party text editor I found that I could use the Microsoft Office utility.
First start off by creating a text editor, below I am using the AJAX toolkit HtmlEditorExtender which is available in the latest toolkit version.
<asp:TextBox id="MyTextBox" TextMode="MultiLine" Height="450px" Width="550px" runat="server"> </asp:textbox>
<ajaxToolKit:HtmlEditorExtender id="MyEditorExtender" TargetControl="MyTextBox" runat="server"> </ajaxToolKit:HtmlEditorExtender>
<as:button id="SpellCheck" text="Check Spelling" onClick="SpellCheck_Click" runat="server"> </asp:button>
Next is the code to call the Word spell check dialog.
First you need to create a reference to the Microsoft Word 12.0 Object Library COM object.
(In the example I am using VB.net but C# is the same.)
Private Sub SpellCheck_Click(sender As Object, e As System.EventsArgs) Hanfles SpellCheck_Click
Dim app as Microsoft.Office.Interop.Word.Application = New Microsoft.Office.Interop.Word.Application()
If Not String.IsNullOrEmpty(MyTextBox.Text) Then
app.Visible = False
Dim template as Object = Missing.Value
Dim newTemplate as Object = Missing.Value
Dim documentType as Object = Missing.Value
Dim visible as Object = False
Dim dlg as Word.Dialog
Dim doc as Microsoft.Office.Interop.Word._Document = app.Documents.Add(template, newTemplate, documentType, visible)
dlg = app.Dialogs.Item(Word.WdWordDialog.wdDialogTollsSpellingAndGrammar)
Dim first As Object = 0
Dim last As Object = doc.Characters.Count - 1
MyTextBox.Text = doc.Range(first, last).Text
Dim saveChanges As Object = False
Dim originalFormat As Object = Missing.Value
Dim routeDocument As Object = Missing.Value
app.Quit(saveChanges, originalFormat, routeDocument)
When clicking the spell check button, the code will call the Microsoft Word’s SpellCheck/Grammar dialog box for display. After all the spelling/grammar errors are corrected, the dialog writes the changes back into the textbox.
This functionality is nice and easy to implement but there are some exceptions that need to be pointed out before making a decision that this solution is right for you.
First, you will need a license copy of MS Word installed on the server that the application is hosted on. Second I have found a KB article written by Microsoft (Considerations for server-side Automation of Office) where Microsoft points out that even though the implementation is easy, there are a number of complications that can occur and that they do not recommend or support server-side Automation of Office.