How to use Microsoft Word's Spellcheck from Vb with out making Word visible.

For the sample code to run you will need to do the following things:
1. Make a reference To Microsoft Word 8.0 Object Library.
2. Add a Textbox and a Listbox.
3. Add 2 Menu Items. Name the first one mnuExit with a caption of &Exit and Name the second one mnuSpellCheck with a Caption ofSpell&Check.


Option Explicit
Public WordApp As Word.Application
Public strText As String
Public WordDoc As Word.Document
Public Spells As SpellingSuggestions
Public spell As SpellingSuggestion
Public bool As Boolean

Private Sub mnuExit_Click()
'This unloads the form
Unload Me
End Sub

Private Sub List1_ItemCheck(Item As Integer)
'This procedure replaces the misspelled word in the textbox with the word selected in the listbox
Text1.Text = List1.List(Item)
End Sub

Private Sub mnuSpellCheck_Click()
'This procedure uses MSWord to spell check the word in the textbox
List1.Clear ' this line clears out the List box
Set WordApp = New Word.Application ' this instanciates the word object
WordApp.Documents.Add 'this adds a document to the documents colletion
Set Spells = WordApp.GetSpellingSuggestions(Text1.Text) 'this gets spelling suggestions on the word in the textbox
bool = WordApp.CheckSpelling(Text1.Text) 'this check if the word is spelled correctly or not
If bool = False Then 'if it isn't it shows the suggestions
'this loops thru Spelling suggestions and add them to the listbox
For Each spell In Spells
List1.AddItem spell.Name
Next spell
Else
MsgBox "Word is spelled correct" 'if it is then it tells you it is correct
End If

WordApp.Quit (0) 'this close word
Set WordApp = Nothing 'this sets the word object to nothing
End Sub

[Home]   [Back to the top]
1