VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



a modification of the microsoft code for using MS Word as a spelling/grammer checker. I found the M

by Chris H (4 Submissions)
Category: String Manipulation
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Wed 14th May 2003
Date Added: Mon 8th February 2021
Rating: (1 Votes)

a modification of the microsoft code for using MS Word as a spelling/grammer checker. I found the MS code caused a problem if the user viewed

Rate a modification of the microsoft code for using MS Word as a spelling/grammer checker. I found the M



' Procedure : SpellCheckMe
' DateTime  : 05/09/2003 16:13
' Author    : Chris Huff - www.tigergreenproductions.com
' Purpose   : Function for calling MS word spelling/grammer function
'               for use in a form.
' Call      : TextBox.text = SpellCheckMe(Textbox.text)
' Desc.     : The below code is a modification of the microsoft code for using
'               MS Word as a spelling/grammer checker.  I found the MS code 
'               caused a problem if the user viewed the spelling dialogue box
'               and then tried to access another application or the VB app with
'               the spelling box open. It caused a 'switch-to' box that locked
'               up the application and would not return control to the spelling
'               dialogue box unless Word was kept visible through the process.
'               Having Word visibly open and close during spell checking was
'               unacceptable.  The result is a new process that provides a
'               solution.
'             It was also determined that if a user canceled the spell checker,
'               that the returned text only contained linebreak characters 
'               within any of the original text.  This issue has also been 
'               resolved.
-
'
Private Function SpellCheckMe(strText As String) As String
    'References.Microsoft Word 8.0 Object Library
    
    'If passed text contains no data then exit function
    If strText = "" Or IsNull(strText) = True Then Exit Function
    
    'txtTemp used to store incoming text in case user cancels spell checker.
    Dim txtTemp As String
    'linebreak used to replace linebreaks that are mis-represented 
    'during spell checking.
    Dim linebreak As String
    linebreak = Chr(13) + Chr(10)
    'store incoming text in temp variable
    txtTemp = strText
    'if error, populate text field with original text.
    On Error GoTo errForm
    'create Word object
    Dim X As Object
    Set X = CreateObject("Word.Application")
    'hide Word - no screen flicker
    X.Visible = False
    'open a new document
    X.Documents.Add
    'minimize window
    X.Application.WindowState = wdWindowStateMinimize
    'make visible to enable 'swith-to' box to work.
    'Word remains minimized
    X.Visible = True
    'pass text to word
    X.Selection.Text = strText
    'run spell/grammar checker
    X.ActiveDocument.CheckGrammar
    'Populate to send back to function call
    'use replace to correct for line break display issue
    '******if user canceled spell check, repopulate with original contents******
    'canceling will return X.selection.text with no text 
    'content but might return line breaks.
    'parse for any character within acceptable alphachar range 
    'to determine if canceled.
    If IsAlpha(X.Selection.Text) = False Then
        SpellCheckMe = strText
    Else
        SpellCheckMe = Replace(X.Selection.Text, Chr(13), linebreak)
    End If
    
    'close word
    'visible to false to minimize screen flicker
    X.Visible = False
    X.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
    'quit Word
    X.Application.Quit
    'release object variable
    Set X = Nothing

    Exit Function
    
errForm:
    SpellCheckMe = strText
    Exit Function

End Function

Public Function IsAlpha(strText As String) As Boolean
    'this section of code obtained from vbcode.com and CRMcG.
    Dim intLen As Integer
    Dim intCounter As Integer
    Dim blnAlpha As Boolean
    Dim strChar As String
    intLen = Len(strText)
    
    'Set the condition of our loop
    For intCounter = 1 To intLen
        strChar = Mid(strText, intCounter, 1)
        'Accept A through z"
        If strChar >= "A" And strChar <= "z" Then
            blnAlpha = True
        Else
            'Accept "spaces", "periods", and "commas"
            If strChar = " " Or strChar = "." Or strChar = "," Then
                blnAlpha = True
            'Reject everything else.
            Else
                blnAlpha = False
            End If
        End If
        'We caught an illegal character (not A-z, " ", ".", or ","
        If blnAlpha = False Then
            IsAlpha = False
            Exit Function
        End If
    Next intCounter
    IsAlpha = True
End Function

Download this snippet    Add to My Saved Code

a modification of the microsoft code for using MS Word as a spelling/grammer checker. I found the M Comments

No comments have been posted about a modification of the microsoft code for using MS Word as a spelling/grammer checker. I found the M. Why not be the first to post a comment about a modification of the microsoft code for using MS Word as a spelling/grammer checker. I found the M.

Post your comment

Subject:
Message:
0/1000 characters