I cannot take credit for this routine, as the bulk of it comes courtesy of Microsoft. It utilizes the Spell Checker in Microsoft Word, and contains a couple of interesting techniques that I have not used before. One is the CoAllowSetForegroundWindow call, which enables the COM server process called to take focus away from the client
application. The other is moving the Word window off screen by setting the top of the window to a large negative number. This prevents it from interfering with the client program. I have tested it with Word 9.0 and word 12.0, and I have implemented it as a module for portability.
J.A. Coutts
Code:Attribute VB_Name = "modSpell" Option Explicit Declare Function CoAllowSetForegroundWindow Lib "ole32.dll" (ByVal pUnk As Object, ByVal lpvReserved As Long) As Long Public Function SpellChk() As String Dim WordApp As Object Dim objDoc As Object 'Word.Document Dim lOrigTop As Long Dim lErr As Long On Error GoTo SpellChkErr ' Create a Word document object Set WordApp = CreateObject("Word.Application") CoAllowSetForegroundWindow WordApp, 0 Set objDoc = WordApp.Documents.Add ' Position Word off screen to avoid having document visible lOrigTop = WordApp.Top WordApp.WindowState = 0 WordApp.Top = -3000 WordApp.Visible = True WordApp.Activate ' Assign the text to the document and check spelling With objDoc .Content.Paste .Activate .CheckSpelling ' After the user has made changes, use the clipboard to ' transfer the contents back to the text box .Content.Copy SpellChk = Clipboard.GetText(vbCFText) ' Close the document and exit Word .Saved = True .Close End With Set objDoc = Nothing WordApp.Visible = False WordApp.Top = lOrigTop WordApp.Quit Set WordApp = Nothing Exit Function SpellChkErr: lErr = err SpellChk = Clipboard.GetText(vbCFText) Screen.MousePointer = vbNormal Select Case lErr Case 91, 429 MsgBox "MS Word cannot be found!", vbExclamation Case Else MsgBox "Error: " & err & " - " & Error$(err), vbExclamation, App.ProductName End Select End Function 'Calling routine Private Sub cmdSpell_Click() Clipboard.Clear Clipboard.SetText txtMessage.Text txtMessage.Text = SpellChk() End Sub


Reply With Quote