VB 6 and CheckSpelling

D

David De Bono

Hi,

I'm developing a VB6 application where we want to use the MS Word
CheckSpelling function to do spell checks in our application.

Code:
CheckSpellingError = Not oWord.CheckSpelling(sWord)

The oWord is a word.application object created with CreateObject

The problem is that the CheckSpelling function is increadible slow. It will
take a minute or so to check through a couple of houndred words.

Any solutions ?

David
 
M

Mark Alexander Bertenshaw

David De Bono said:
Hi,

I'm developing a VB6 application where we want to use the MS Word
CheckSpelling function to do spell checks in our application.

Code:
CheckSpellingError = Not oWord.CheckSpelling(sWord)

The oWord is a word.application object created with CreateObject

The problem is that the CheckSpelling function is increadible slow. It will
take a minute or so to check through a couple of houndred words.

David -

It's probably slow because it is having to load a whole new instance of Word
each time, and is then doing out of process calls. You could try keeping
the instance of Word open all the time. In theory, you could work out the
interface to the office spelling and thesaurus component - but the problem
is that it is a DLL which uses C type calls. I've been down this road. It
is painful.

Otherwise, use a 3rd party spell check control (which uses its own
dictionaries). Annoying, true, but until Microsoft unbundles its spell
check routines, there is no other way.
 
D

David De Bono

Hi Mark,

I don't unload the word.application object each time. And I have also
noticed that when calling CheckSpelling it use almost all the processor
resources.

David
 
M

MikeD

It still appears as if you're checking the spelling of one word at a time.
Here's a class module that I use:

-----BEGIN MODULE
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "clsSpellCheck"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

Private m_sTextToCheck As String
Private m_sTextCorrected As String

Public Enum PerformSpellCheckConstants
NoSpellingErrors = 1
CorrectionsMade = 2
CorrectionsNotMade = 3
AutomationError = 4
End Enum

Public Function PerformSpellCheck() As PerformSpellCheckConstants

Dim oWordApp As Word.Application
Dim oWordDoc As Word.Document
Dim bNoSpellingErrors As Boolean
Dim sTemp As String

On Error GoTo EH

Set oWordApp = New Word.Application

If Not oWordApp Is Nothing Then
Set oWordDoc = oWordApp.Documents.Add
End If

If oWordDoc Is Nothing Then
PerformSpellCheck = AutomationError

If Not oWordApp Is Nothing Then
oWordApp.Quit wdDoNotSaveChanges
Set oWordApp = Nothing
End If

Exit Function
End If

'Let's make sure there are spelling errors
bNoSpellingErrors = oWordApp.CheckSpelling(TextToCheck, , False)

If bNoSpellingErrors Then
PerformSpellCheck = NoSpellingErrors

'Make sure the original text is assigned to TextCorrected property
'in case this method's return value is not checked
m_sTextCorrected = TextToCheck

oWordDoc.Close wdDoNotSaveChanges
Set oWordDoc = Nothing

oWordApp.Quit wdDoNotSaveChanges
Set oWordApp = Nothing

Exit Function
End If

With oWordDoc
.Content.Text = TextToCheck
.Activate
.CheckSpelling , False, True
m_sTextCorrected = .Content.Text

'Word replaces CR/LFs with a CR and/or may append a CR to the end of
the text.
'This needs fixed.
m_sTextCorrected = Replace$(m_sTextCorrected, vbCr, vbCrLf, 1, -1,
vbBinaryCompare)

'Eliminate trailing CR/LFs from end of string. It's possible there
could be
'multiple pairs.
Do While Right$(m_sTextCorrected, 2) = vbCrLf
m_sTextCorrected = Left$(m_sTextCorrected,
Len(m_sTextCorrected) - 2)
Loop

'Now, we need to append the same number of CR/LFs as the original
string had.
sTemp = TextToCheck 'use temp variable so as not to modify original
string
Do While Right$(sTemp, 2) = vbCrLf
m_sTextCorrected = m_sTextCorrected & vbCrLf
sTemp = Left$(sTemp, Len(sTemp) - 2)
Loop

If StrComp(m_sTextCorrected, TextToCheck, vbBinaryCompare) = 0 Then
'If the strings are identical, corrections were not made because
'we already know there were spelling errors.
PerformSpellCheck = CorrectionsNotMade
Else
PerformSpellCheck = CorrectionsMade
End If
End With


'Make sure we clean up after ourselves
If Not oWordDoc Is Nothing Then
oWordDoc.Close wdDoNotSaveChanges
Set oWordDoc = Nothing
End If

If Not oWordApp Is Nothing Then
oWordApp.Quit wdDoNotSaveChanges
Set oWordApp = Nothing
End If

Exit Function

EH:

If Not oWordDoc Is Nothing Then
oWordDoc.Close wdDoNotSaveChanges
Set oWordDoc = Nothing
End If

If Not oWordApp Is Nothing Then
oWordApp.Quit wdDoNotSaveChanges
Set oWordApp = Nothing
End If

PerformSpellCheck = AutomationError


End Function


Public Property Get TextCorrected() As String

TextCorrected = m_sTextCorrected

End Property

Public Property Get TextToCheck() As String

TextToCheck = m_sTextToCheck

End Property

Public Property Let TextToCheck(sText As String)

m_sTextToCheck = sText

End Property
-----END MODULE

And here's an example of using it:


Dim oSpellCheck As clsSpellCheck
Set oSpellCheck = New clsSpellCheck

With oSpellCheck
.TextToCheck = Text1.Text
Select Case .PerformSpellCheck
Case CorrectionsMade
Text1.Text= .TextCorrected
Case AutomationError
MsgBox "Unable to perform spell check due to an
Automation error", vbCritical
End Select
End With

Set oSpellCheck = Nothing


Mike
 
S

Saucer Man

Hi Mike.

This is very nice. I notice that when the spell check box closes, the whole
screens blanks out for a second. Am I doing something wrong or is there
soemthing that I should look for? Thanks.

Rich
 
M

MikeD

Saucer Man said:
Hi Mike.

This is very nice. I notice that when the spell check box closes, the whole
screens blanks out for a second. Am I doing something wrong or is there
soemthing that I should look for? Thanks.

Rich

Thanks. Can't help you though because I've never noticed the screen
blanking, nor has this ever been reported to me.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top