Spell Checker (uses MS Word)

By | 2002-06-01

This code uses OLE Automation to allow VB to open an instance of MS Word if the user has it on their system and spell check the contents of a text box. It could easily be modified to work with any control that has text on it. I would recommend better error control than the On Error statement listed here.

Original Author: T. Heinel

Assumptions

Just create the text box and command button listed in the code.

Returns

The code automatically replaces the original text of the message box with the corrected text.

Code

Private Sub cmdSpellCheck_Click()
  'On Error Resume Next 'Best to un-comment this while testing
  Dim objMsWord As Word.Application
  Dim strTemp As String
  Set objMsWord = CreateObject("Word.Application")
  objMsWord.WordBasic.FileNew
  objMsWord.WordBasic.Insert txtMessage.Text
  objMsWord.WordBasic.ToolsSpelling
  objMsWord.WordBasic.EditSelectAll
  objMsWord.WordBasic.SetDocumentVar "MyVar", objMsWord.WordBasic.Selection
  objMsWord.Visible = False ' Mostly prevents Word from being shown
  strTemp = objMsWord.WordBasic.GetDocumentVar("MyVar")
  txtMessage.Text = Left(strTemp, Len(strTemp) - 1)
  
  objMsWord.Documents.Close (0) ' Close file without saving
  objMsWord.Quit         ' Exit Word
  Set objMsWord = Nothing    ' Clear object memory
  frmMain.SetFocus        ' Return focus to Main form
End Sub

Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.