Re: VB 6 and CheckSpelling

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: MikeD (nobody_at_nowhere.edu)
Date: 10/08/04


Date: Thu, 7 Oct 2004 20:48:21 -0400

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

"David De Bono" <er_fortsatt@hotmail.com> wrote in message
news:%23nSnA8IrEHA.644@tk2msftngp13.phx.gbl...
> 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
>
> "Mark Alexander Bertenshaw" <mark.bertenshaw@virgin.net> wrote in message
> news:u$urOjFrEHA.3896@TK2MSFTNGP15.phx.gbl...
> >
> > "David De Bono" <er_fortsatt@hotmail.com> wrote in message
> > news:uK5Npt$qEHA.4008@TK2MSFTNGP14.phx.gbl...
> >> 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.
> >
> > --
> > Mark Bertenshaw
> > LEAX Controls Ltfd
> > UK
> >
> >
>
>



Relevant Pages

  • Re: Open File Dialog Filter
    ... Double checked class module name, ... >>> Dim cFileOpen As clsGetOpenFileName ... As String ... Public Property Let FileType ...
    (microsoft.public.excel.programming)
  • Re: VB 6 and CheckSpelling
    ... Here's a class module that I use: ... Private m_sTextCorrected As String ... Public Function PerformSpellCheckAs PerformSpellCheckConstants ... Public Property Get TextCorrectedAs String ...
    (microsoft.public.office.developer.vba)
  • Re: VB 6 and CheckSpelling
    ... Here's a class module that I use: ... Private m_sTextCorrected As String ... Public Function PerformSpellCheckAs PerformSpellCheckConstants ... Public Property Get TextCorrectedAs String ...
    (microsoft.public.word.vba.general)
  • Error: no parameterless constructor defined for this object
    ... Private _forename As String ... Private _hrReceivedEmails As Boolean ... Public Property Forename() As String ...
    (microsoft.public.dotnet.framework.aspnet.datagridcontrol)
  • No parameterless constructor error for dataview
    ... Private _forename As String ... Private _hrReceivedEmails As Boolean ... Public Property Forename() As String ...
    (microsoft.public.dotnet.framework.aspnet)