The Microsoft Word Object Model contains a lot of capabilities that .NET developers can benefit from. While everybody knows about the abilities of Microsoft Word related to text formatting, few are aware of its rich capacity in the spell check area.
Let’s try to make use of this capacity now. We will need a Microsoft Word COM addin in which we use an ordinary .net form with the RichTextBox control and two Buttons. You type some text into the RichTextBox, later on this text will be spell checked on a button click. The code of button click event handler is shown below:
Private Sub btnRun_Click(ByVal sender As System.Object, ByVal e _
As System.EventArgs) Handles btnRun.Click
Dim addinModuleObj As AddinModule = TryCast _
(AddinModule.CurrentInstance, AddinModule)
richTextBox.SelectAll()
richTextBox.SelectionColor = Color.Black
Dim Template As Object = Type.Missing
Dim NewTemplate As Object = Type.Missing
Dim DocumentType As Object = Type.Missing
Dim Visible As Object = False
Dim docs As Word.Documents = addinModuleObj.WordApp.Documents
If docs IsNot Nothing Then
Try
Dim document As Word._Document = docs.Add(Template, _
NewTemplate, DocumentType, Visible)
If document IsNot Nothing Then
Try
Dim range As Word.Range = document.Content
If range IsNot Nothing Then
Try
range.Text = richTextBox.Text
Dim Count As Integer = _
FindSpellingErrors(document)
Dim SaveChanges As Object = _
Word.WdSaveOptions.wdDoNotSaveChanges
Dim OriginalFormat As Object = Type.Missing
Dim RouteDocument As Object = Type.Missing
document.Close(SaveChanges, OriginalFormat, _
RouteDocument)
If Count = 0 Then
MessageBox.Show("There are no errors!", _
"Add-in Express HowTo")
Else
MessageBox.Show(String.Format( _
"{0} Error(s) were found!", Count), _
"Add-in Express HowTo")
End If
Finally
Marshal.ReleaseComObject(range)
End Try
End If
Finally
Marshal.ReleaseComObject(document)
End Try
End If
Finally
Marshal.ReleaseComObject(docs)
End Try
End If
End Sub
And here is the FindSpellingErrors method used in the code above:
Private Function FindSpellingErrors(ByVal document As Word._Document) _
As Integer
Dim ret As Integer = 0
document.SpellingChecked = False
Dim spellingErrors As Word.ProofreadingErrors = TryCast( _
document.SpellingErrors, Word.ProofreadingErrors)
If spellingErrors IsNot Nothing Then
Try
ret = spellingErrors.Count
For i As Integer = 1 To spellingErrors.Count
Dim itemError As Word.Range = spellingErrors.Item(i)
If itemError IsNot Nothing Then
Try
richTextBox.Find(itemError.Text, _
itemError.Start, _
RichTextBoxFinds.MatchCase Or _
RichTextBoxFinds.WholeWord)
richTextBox.SelectionColor = Color.Red
Finally
Marshal.ReleaseComObject(itemError)
End Try
End If
Next
Finally
Marshal.ReleaseComObject(spellingErrors)
End Try
End If
Return (ret)
End Function
There is one unpleasant thing about the ProofreadingErrors collection – it is slow. I don’t know the exact reason why it is so, I just feel a caveat should be made about this.
Also note that you can use the capabilities of the Word Object Model not only from inside of a COM add-in, but in a standalone application as well.
You may also be interested in:
Creating Word COM add-in step-by-step
Available downloads:
The sample add-ins below were written using Add-in Express 2008 for Office and .net
C# sample Word add-in for VS 2005
VB.NET sample Word add-in for VS 2005

del.icio.us
Digg
Stumbleupon