Spell Check Under Access Runtime
Public Sub RunTimeSpellChk(frmForm As Form)
' Purpose   Perform a Spell Check in the active control of the form passed
'           Uses Late Binding - Does not need a reference to Microsoft Word
'           Requires Microsoft Word's Spelling engine
'           Should only get called when running on an Access RunTime installation
' Author    Ron Weiner    rweiner@WorksRite.com
'           Copyrite 2004-2006 WorksRite Software Solutions
'           You may use this code example for any purpose what-so-ever with
'           acknowledgement. However, you may not publish the code without
'           the express, written permission of the author.

    Dim objTmpDocAs Object
    Dim lngLeft As Long, lngTop As Long

    On Error GoTo err_SpellCheck
    If frmForm.ActiveControl.ControlType = acTextBox Then
        DoCmd.Hourglass True
        DoEvents
err_SpellCheckRetry:

    ' gobjWord is declared as a Public object in a BAS module
    ' and once created lives the lifetime of the application.
    If gobjWord Is Nothing Then
            ' Start word running for our app
            Set gobjWord = CreateObject("Word.Application")
        End If
        ' Don't want to See Word no way - no how
        gobjWord.Visible = False
        ' Remember the screen coordinates where the Word Doc opened
        lngLeft = gobjWord.Left
        lngTop = gobjWord.Top
        ' Copy the contents of the controls text to the clipboard
        frmForm.ActiveControl.SetFocus
        If Len(frmForm.ActiveControl.Text) > 0 Then
            ' If there is text in the text box then we can start
            frmForm.ActiveControl.SelStart = 0
            frmForm.ActiveControl.SelLength = Len(frmForm.ActiveControl.Text)
            DoCmd.RunCommand (acCmdCopy)
            ' Create a Word document object...
            Set objTmpDoc = gobjWord.Documents.Add
            ' Move the Word Doc WAY Off the screen so it dosent distract our user
            gobjWord.WindowState = 0
            gobjWord.Left = -3000
            gobjWord.Top = -3000
            ' Assign the text to the document and Check for spelling errors ...
            With objTmpDoc
                .Content.Paste
                ' Without next line sometimes word does NOT visiblize the spell check dialog
                ' Since word is off the screen we don't even get a flicker
                gobjWord.Visible = True
                DoEvents
                .CheckSpelling
                gobjWord.Visible = False
                .Content.Copy
            End With
            ' Put MS Word back where we found it
            gobjWord.Left = lngLeft
            gobjWord.Top = lngTop
            ' Paste the text back into the text box
            DoCmd.RunCommand (acCmdPaste)
        End If
        ' Let 'em know we are done
        MsgBox "The spelling check is complete.", vbInformation, "RunTime Spell Check"
err_SpellCheckOut:
        ' Destroy Word document object
        On Error Resume Next
        objTmpDoc.Saved = True
        objTmpDoc.Close
        Set objTmpDoc = Nothing
        DoCmd.Hourglass False
    End If
    Exit Sub
err_SpellCheck:
    ' Error handler
    Select Case Err.Number
        Case 429   ' Looks like Word might not be installed on this box
            MsgBox "Error " & Err.Number & vbCrLf & Err.Description & vbCrLf _
            & "Microsoft Word Not Installed", vbExclamation, "Spell Check Error"
        Case 462   ' Something musta' happened to our instance of word.
                   ' So lets see if we can start another one
            Set gobjWord = Nothing
            Resume err_SpellCheckRetry
        Case Else   ' Something else happened
            MsgBox "Error " & Err.Number & vbCrLf & Err.Description, vbExclamation, "Spell Check Error"
    End Select
    Resume err_SpellCheckOut
End Sub

 

Copyright 2001 WorksRite Software Solutions -- Last Updated 06/13/05
Problems with this site? Please contact the Webmaster@WorksRite.com with your comments, questions, or suggestions.