The text of this article is not in this database — only its details are. The old site it was published on is gone, but the Internet Archive kept a copy: Word 2007 Spellchecker Functionality in C#
1 Comment
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment.

Joshua FredricksonPosted Jun 7, 2011, 2:31 PM
Thanks for the original code. I've modified for my needs in VB and thought I would post it here for VB users. Public Class Spellchecker Dim wrdApp As Microsoft.Office.Interop.Word.Application Public Sub New() 'Initialize a new session of Word for spellchecking purposes wrdApp = New Microsoft.Office.Interop.Word.Application 'Adds a new document to the Word session wrdApp.Documents.Add() End Sub Public Sub Dispose() 'Cleanup wrdApp.Documents.Close(Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges) wrdApp.Quit(False) End Sub ''' <summary> ''' Spellchecker which accepts a string and returns an Arraylist with spelling errors found. ''' </summary> ''' <param name="Text">Word(s) to Spellcheck</param> ''' <returns>Arraylist of spelling errors</returns> Public Function SpellCheck(ByVal Text As String) As ArrayList Dim WordList As New ArrayList Dim wrdRange As Microsoft.Office.Interop.Word.Range Dim spellcollection As Microsoft.Office.Interop.Word.ProofreadingErrors Try 'Clears word document wrdApp.ActiveDocument.Bookmarks("\Page").Range.Text = "" 'Adds the text to be spellchecked wrdRange = wrdApp.ActiveDocument.Range() wrdRange.InsertAfter(Text) 'Gets a collection strings/words with spelling errors spellcollection = wrdRange.SpellingErrors 'Verifies spelling errors were found If (spellcollection.Count > 0) Then Dim i As Integer = 1 'Counter for Loop 'Loops through errors and adds each word to the Arraylist Do While i <= spellcollection.Count Dim Word As String = spellcollection(i).Text If Not Word = String.Empty Then WordList.Add(Word) i += 1 Loop End If 'Returns the words with spelling errors as an Arraylist Return WordList Catch ex As Exception 'No spelling errors found, returns nothing Return Nothing Finally 'Cleanup wrdRange = Nothing WordList = Nothing spellcollection = Nothing End Try End Function ''' <summary> ''' Returns an Arraylist of suggested corrections for a word provided. ''' </summary> ''' <param name="Text">Word to be corrected</param> ''' <returns>Arraylist of Suggestions</returns> Public Function Suggestions(ByVal Text As String) As ArrayList Dim CorrectionsCollection As Microsoft.Office.Interop.Word.SpellingSuggestions Dim WordList As New ArrayList Try 'Gets a collection of Spelling Suggestions for word supplied. CorrectionsCollection = wrdApp.GetSpellingSuggestions(Text) 'Verifies that there are suggestions were found If (CorrectionsCollection.Count > 0) Then Dim i As Integer = 1 'Counter for Loop 'Loops through Suggestions and add each Suggestion to an Arraylist Do While i <= CorrectionsCollection.Count Dim Word As String = CorrectionsCollection(i).Name If Not Word = String.Empty Then WordList.Add(Word) i += 1 Loop Else WordList.Add("No Suggestions!") End If 'Returns the Arraylist of Suggestions Return WordList Catch ex As Exception Return Nothing Finally CorrectionsCollection = Nothing WordList = Nothing End Try End Function End Class