Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using MSWORD SpellCheck, working but....

Status
Not open for further replies.

JoseC

Programmer
Dec 11, 2001
18
0
0
I got the code below to work. I even copy the current clipboard content and restore it later 'cause the spellcheck process erases it.
Anyhow, at times; there will be a delay or something that will display a "SWITCH TO application" or "RETRY" message box that appears to the user. My current assumption is that perhaps the CreateObject took a while and could open the instance of WORD.
How do I increase the timeout??
or what do I do so the user doesn't see that?

'----------------------------------------------------------
Function MsSpellCheck(strText As String) As String
'*** Before doing spell check, copy any ClipBoard text to a variable
'*** for temporary storage.
Dim tempClipBoardText As String
tempClipBoardText = Clipboard.GetText(1)
'MsgBox tempClipBoardText

'*** The MsSpellCheck will perform the following:
'*** 1. Create an object instance of MS WORD
'*** 1. Start a new document
'*** 1. Paste the passed text onto the document
'*** 1. Select all text in document
'*** 1. Perform spell check
'*** 1. Copy all now corrected text onto clipboard
'*** 1. Set text from clipboard onto variable
'*** 1. Close MS WORD
'*** 1. Destroy MS WORD object instance
Dim oWord As Object
Dim strSelection As String
Set oWord = CreateObject("Word.Basic")
oWord.AppMinimize
MsSpellCheck = strText
oWord.FileNewDefault
oWord.EditSelectAll
oWord.EditCut
oWord.Insert strText
oWord.StartOfDocument
On Error Resume Next
oWord.ToolsSpelling
On Error GoTo 0
oWord.EditSelectAll
strSelection = oWord.Selection$

If Mid(strSelection, Len(strSelection), 1) = Chr(13) Then
strSelection = Mid(strSelection, 1, Len(strSelection) - 1)
End If


If Len(strSelection) > 1 Then
MsSpellCheck = strSelection
End If
oWord.FileCloseAll 2
oWord.AppClose
Set oWord = Nothing

'*** After doing spell check, copy any variable text onto ClipBoard
'*** that way, user will have any before MsSpellCheck process ClipBoard text back again.
Clipboard.SetText tempClipBoardText, 1


End Function
'----------------------------------------------------------
 
This is what I use and have not found any problems.
Public Function fncSpellCheck(ByVal strText As String) As String
Dim oWord As Object
Dim i As Integer
Dim strSelection As String

Set oWord = CreateObject("Word.Basic")
oWord.AppMinimize
fncSpellCheck = strText
oWord.FileNewDefault
oWord.EditSelectAll
oWord.EditCut
oWord.Insert strText
oWord.StartOfDocument
On Error Resume Next
oWord.ToolsSpelling
On Error GoTo 0
oWord.EditSelectAll
strSelection = oWord.Selection$


'------------------------------------------------------------------------
' Carriage return character Chr(13) or vbCr must be replaced with the
' line feed character vbLf and also have the trailing character trimmed
'------------------------------------------------------------------------

i = InStr(strSelection, vbCr)
Do Until i = 0
strSelection = Left(strSelection, i) & vbLf & Mid(strSelection, i + 1)
i = InStr(i + 2, strSelection, vbCr)
Loop

If Mid(strSelection, Len(strSelection), Len(strSelection)) = vbLf Then
strSelection = Mid(strSelection, 1, Len(strSelection) - 2)
End If

If Len(strSelection) > 1 Then
fncSpellCheck = strSelection
End If

oWord.FileCloseAll 2
oWord.AppClose
Set oWord = Nothing
End Function
 
NJLDoc,
Thanks for your reply. They look very similar.
What I am thinking is that, those PCs are running multiple applications that is slowing down the loading of WORD and therefore giving me a timeout on the OLE connection.
I haven't seen it on PCs that are running just my App or less Apps at the same time.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top