Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / January 2005

Tip: Looking for answers? Try searching our database.

VB 6 and CheckSpelling

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
David De Bono - 07 Oct 2004 00:23 GMT
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
Mark Alexander Bertenshaw - 07 Oct 2004 11:33 GMT
> Hi,
>
[quoted text clipped - 8 lines]
> 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.

--
Mark Bertenshaw
LEAX Controls Ltfd
UK
David De Bono - 07 Oct 2004 17:59 GMT
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

>> Hi,
>>
[quoted text clipped - 29 lines]
> LEAX Controls Ltfd
> UK
MikeD - 08 Oct 2004 01:48 GMT
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

> Hi Mark,
>
[quoted text clipped - 37 lines]
> > LEAX Controls Ltfd
> > UK
Saucer Man - 12 Jan 2005 20:35 GMT
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

> It still appears as if you're checking the spelling of one word at a time.
> Here's a class module that I use:
[quoted text clipped - 220 lines]
> > > LEAX Controls Ltfd
> > > UK
MikeD - 12 Jan 2005 20:47 GMT
> Hi Mike.
>
[quoted text clipped - 3 lines]
>
> Rich

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

Signature

Mike
Microsoft MVP Visual Basic

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.