Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleCall Word to spell check from Visual Basic
KeywordsOffice, Word, spell, spellcheck
CategoriesOffice
 
Create a Word.Basic object an invoke its methods. Use FileNew to create a new document, Insert to insert the text you want to check, ToolsSpelling to check the spelling, EditSelectAll to select all of the results, and FileExit to close the new document.
 
Private Sub Command1_Click()
Dim speller As Object
Dim txt As String
Dim new_txt As String
Dim pos As Integer
        
    On Error GoTo OpenError
    Set speller = CreateObject("Word.Basic")
    On Error GoTo 0
    
    speller.FileNew
    speller.Insert Text1.Text
    speller.ToolsSpelling
    speller.EditSelectAll
    txt = speller.Selection()
    speller.FileExit 2

    If Right$(txt, 1) = vbCr Then _
        txt = Left$(txt, Len(txt) - 1)
    new_txt = ""
    pos = InStr(txt, vbCr)
    Do While pos > 0
        new_txt = new_txt & Left$(txt, pos - 1) & vbCrLf
        txt = Right$(txt, Len(txt) - pos)
        pos = InStr(txt, vbCr)
    Loop
    new_txt = new_txt & txt
    
    Text1.Text = new_txt
    Exit Sub
    
OpenError:
    MsgBox "Error" & Str$(Error.Number) & _
        " opening Word." & vbCrLf & _
        Error.Description
End Sub
 
Note that this program uses late binding to create the Word.Basic object. To use early binding, declare the object as type Word.Application (requires a reference to the Word object library--use Project\References). This will give slightly better performance and will allow Intellisense to work on the object.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated