Article ID: 243844 - Last Review: May 28, 2008 - Revision: 8.1

How to automate Word from Visual Basic or Visual Basic for Applications for spell checking

This article was previously published under Q243844

On This Page

Expand all | Collapse all

SUMMARY

Software written to work with Microsoft Office can take advantage of the spell checking capabilities of Microsoft Word to add spell checking to their own application.

Word's Automation model contains a CheckSpelling function that lets you check the spelling of a document hosted in Word. By using Word Automation, developers can dynamically create a new document, add some text they want to check, and then have Word check the spelling. This article shows you how to automate Word to provide this functionality.

MORE INFORMATION

You can use this code sample from either Microsoft Visual Basic or Microsoft Visual Basic for Applications without any changes. However, the sample assumes that you are using a Visual Basic client to create a new project.

Creating a spell check client

  1. Start Visual Basic and create a new Standard EXE project. Form1 is created by default.
  2. Add a TextBox control and CommandButton to Form1.
  3. In the code window for Form1, add the following code:
    Option Explicit
    
    Private Declare Function CoAllowSetForegroundWindow Lib "ole32.dll" (ByVal pUnk As Object, ByVal lpvReserved As Long) As Long
    
    Private Sub Command1_Click()
        Dim oWord As Object
        Dim oTmpDoc As Object
        Dim lOrigTop As Long
        
        ' Create a Word document object
        Set oWord = CreateObject("Word.Application")
        
        CoAllowSetForegroundWindow oWord, 0
    
        Set oTmpDoc = oWord.Documents.Add
        ' Position Word off screen to avoid having document visible
        lOrigTop = oWord.Top
        oWord.WindowState = 0
        oWord.Top = -3000
        
        oWord.Visible = True
        oWord.Activate
        
        ' Copy the contents of the text box to the clipboard
        Text1.SelStart = 0
        Text1.SelLength = Len(Text1.Text)
        Clipboard.Clear
        Clipboard.SetText Text1.SelText
        
        ' Assign the text to the document and check spelling
        
        With oTmpDoc
            .Content.Paste
            .Activate
            .CheckSpelling
            
            ' After the user has made changes, use the clipboard to
            ' transfer the contents back to the text box
            .Content.Copy
            Text1.Text = Clipboard.GetText(vbCFText)
            ' Close the document and exit Word
            .Saved = True
            .Close
        End With
        Set oTmpDoc = Nothing
        
        oWord.Visible = False
        
        oWord.Top = lOrigTop
        oWord.Quit
        Set oWord = Nothing
    End Sub
    
  4. Compile and run the program. Press the Command1 command button to run the spell check. Word's spell check dialog box appears to confirm the spelling of the words "mispelled", "textt", "receive", and "resultes". After you correct the misspelled words, the text is returned to the text box.

APPLIES TO
  • Microsoft Office Word 2007
  • Microsoft Office Word 2003
  • Microsoft Word 2002
  • Microsoft Word 2000
  • Microsoft Word 97 Standard Edition
  • Microsoft Visual Basic 4.0 Professional Edition
  • Microsoft Visual Basic 5.0 Professional Edition
  • Microsoft Visual Basic 6.0 Professional Edition
  • Microsoft Visual Basic 4.0 Enterprise Edition
  • Microsoft Visual Basic 5.0 Enterprise Edition
Keywords: 
kbautomation kbhowto KB243844
 

Article Translations

 

Related Support Centers