This is a little class I used to make a spell checker.
The hard part is trying to navigate the Word library.
Of course, the first step was to make an Application object.
My first try was to use CheckSpelling, but CheckSpelling returns a Boolean, while I wanted a word.
On the second try, I found GetSpellingSuggestion, which returns a SpellingSuggestions object.
This could be used to eventually get the string containing a better spelling.
The problem was how to call it. Calling it immediately after creating the new document raised an error that
said 'this can't be done without a document open'.
So, the next step was how to open a document. I looked for an .Open method, but I didn't find it. I then
foolishly tried to create a new document instance and add it to the Application's Documents by setting the read-only
ActiveDocument, looking for .CreateDocument, which doesn't exists, and the more fruitful failure, trying to add it into the
.Documents class collection. Finally, I decided to call Documents.Add with no arguments (since they were all optional) and
this worked. The spell checker was complete.
After re-organizing my code into a nice class, I returned to the .CheckSpelling method and found that it indeed returns
True if the word is spelled correctly. This means that correctly spelled words can pass right through the SpellCheck function.
Note that you have to add a reference to Word. Go to Solution Explorer, References, right click and go to Add Reference. Click on the
COM tab and wait for a bit... scroll down until you get to Microsoft Word ##.#, and add that reference. After that, things should work.
Public Class WordCheck
'working version 2.
'So, there's a dispose method because I would like these Word objects to be cleaned ASAP
Dim WD As Word.Application
'Provides basic word functionality.
Dim Sugg As Word.SpellingSuggestions
'Will hold a list of spelling suggestions.
Public Sub New()
WD = New Word.Application
'Have to create a new document, otherwise we can't check for spelling suggestion.
Public Function SpellCheck(ByVal Word As String) As String
If WD.CheckSpelling(Word) Then
'This returns true if the word is spelled correctly.
Sugg = WD.GetSpellingSuggestions(Word)
'Get list of spelling suggestion.
If Sugg.Count >= 1 Then
'Get the first one, this is most likely to be correct.
'Otherwise, no results were matched: the word is either correct or horribly wrong.
Public Sub Dispose() Implements System.IDisposable.Dispose
Sugg = Nothing
WD = Nothing
'Close and quit everything.