Chức năng spelling/grammer của Microsoft Word rất tiện dụng và mạnh mẽ. Làm thế nào để làm cho chương trình của mình có chức năng này ? Sao bạn không thử tích hợp chức năng này của Word vào chương trình của mình. Nếu thích mời các bạn tham khảo.
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
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
'tạo Word object
Dim X As Object
Set X = CreateObject(''Word.Application'')
'dấu Word
X.Visible = False
'mở một new document
X.Documents.Add
'thu nhỏ cửa sổ
X.Application.WindowState = wdWindowStateMinimize
X.Visible = True
'chuyển text sang word
X.Selection.Text = strText
'chạy spell/grammar checker
X.ActiveDocument.CheckGrammar
If IsAlpha(X.Selection.Text) = False Then
SpellCheckMe = strText
Else
SpellCheckMe = Replace(X.Selection.Text, Chr(13), linebreak)
End If
'đóng word
X.Visible = False
X.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
X.Application.Quit
Set X = Nothing
Exit Function
errForm:
SpellCheckMe = strText
Exit Function
End Function
Public Function IsAlpha(strText As String) As Boolean
Dim intLen As Integer
Dim intCounter As Integer
Dim blnAlpha As Boolean
Dim strChar As String
intLen = Len(strText)
For intCounter = 1 To intLen
strChar = Mid(strText, intCounter, 1)
If strChar >= ''A'' And strChar <= ''z'' Then
blnAlpha = True
Else
If strChar = '' '' Or strChar = ''.'' Or strChar = '','' Then
blnAlpha = True
Else
blnAlpha = False
End If
End If
If blnAlpha = False Then
IsAlpha = False
Exit Function
End If
Next intCounter
IsAlpha = True
End Function