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.

[html]<br /><html><br /> <asp:TextBox id="MyTextBox" TextMode="MultiLine" Height="450px" Width="550px" runat="server"> </asp:textbox><br /> <ajaxToolKit:HtmlEditorExtender id="MyEditorExtender" TargetControl="MyTextBox" runat="server"><br /> </ajaxToolKit:HtmlEditorExtender><br /> <as:button id="SpellCheck" text="Check Spelling" onClick="SpellCheck_Click" runat="server"></asp:button><br /></html><br />[/html]

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.)

[vbnet]<br />Imports Microsoft.Office.Interop<br />Imports System.Reflection<br /><br />Private Sub SpellCheck_Click(sender As Object, e As System.EventsArgs) Hanfles SpellCheck_Click<br /> Dim app as Microsoft.Office.Interop.Word.Application = New Microsoft.Office.Interop.Word.Application()<br /> If Not String.IsNullOrEmpty(MyTextBox.Text) Then<br /> app.Visible = False<br /> Dim template as Object = Missing.Value&lt;br /&gt;<br /> Dim newTemplate as Object = Missing.Value&lt;br /&gt;<br /> Dim documentType as Object = Missing.Value&lt;br /&gt;<br /> Dim visible as Object = False&lt;br /&gt;<br /> Dim dlg as Word.Dialog<br /> Dim doc as Microsoft.Office.Interop.Word._Document = app.Documents.Add(template, newTemplate, documentType, visable)<br /> doc.Words.First.InsertBefore(Server.HtmlDecode(MyTextBox.Text))&lt;br /&gt;<br /> doc.Activate()<br /> dlg = app.Dialogs.Item(Word.WdWordDialog.wdDialogTollsSpellingAndGrammar)&lt;br /&gt;<br /> dlg.Show()<br /><br /> Dim first As Object = 0<br /> Dim last As Object = doc.Characters.Count – 1<br /> MyTextBox.Text = doc.Range(first, last).Text<br /> End If<br /><br /> Dim saveChanges As Object = False<br /> Dim originalFormat As Object = Missing.Value<br /> Dim routeDocument As Object = Missing.Value<br /> app.Quit(saveChanges, originalFormat, routeDocument)<br />End Sub<br />[/vbnet]

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.

Need to test the height of this box....

Social Wall