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 / Spelling and Grammar / April 2005

Tip: Looking for answers? Try searching our database.

Problem with GoToNext(what:=wdGoToSpellingError)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Howard Kaikow - 06 Apr 2005 14:26 GMT
I am using Word 2003 for this problem. I have not tried with earlier
versions.

Using the code below, where rngError is of type Word.Range, strError is of
type string and lngLastStart is of type Long, I find that the GoToNext fails
when there is punctuation  (e.g., comma, period, or slash) immediately after
the spelling error.

In that case,  one of two things happens:

1. If the spelling error is the first word in the document, that spelling
error is found, but no others are found.
2. If the spelling error is not the first word in the document, then all
spelling errors up to, but not including that error, are found, and no
errors after that spelling error are found.

I did not find anything in the MSFT KB on this issue.

The same problem occurs if I use GoToNext with the Range object, instead of
the Selection object.

The problem does not occur using the ProofReadingErrors collection, only
when navigating with GoToNext(what:=wdGoToSpellingError).

Any ideas on a workaround, without using the ProofReadingErrors collection?

           lngLastStart = -1
           Do
               Set rngError = Selection.GoToNext(what:=wdGoToSpellingError)
               With rngError
                   If .Start = lngLastStart Then
                       Exit Do
                   End If
                   strError = .Text
                   lngLastStart = .Start
                   .Collapse direction:=wdCollapseEnd
                   .Select
               End With
           Loop
Howard Kaikow - 08 Apr 2005 06:25 GMT
The following is amore complete statement of the problem and generates the
test documents.

Option Explicit
' Author: Howard Kaikow
' Author URL: http://www.standards.com/
' Date: April 2005
   Private docNew As Word.Document
   Private yourErrors As Word.ProofreadingErrors
Private Sub wdGoToSpellingErrorBug()
' Purpose: This code demonstrates the problem described below.
'          The code generates test documents and displays results using
Debug.Print.
'          The generated documents are not saved.

' Problem: Navigating through a document using GoToNext or GoTo with
wdGoToSpellingError
'          fails to find all spelling errors if there is punctuation (e.g.,
comma, period, or slash)
'          immediately after any of the spelling errors in the document.

'          The problem occurs using either the Range or Selection objects.

'          The problem does not occur using the ProofReadingErrors
collection, only
'          when navigating with wdGoToSpellingError.

' Word versions: The problem exists in Word 97, Word 2000, Word 2002 and
Word 2003.

' Problem behavior:
'   1. If a spelling error, with immediately following punctuation, is the
first word in the document,
'      that spelling error is found, but no others are found.

'   2. If a spelling error, with immediately following punctuation, is not
the first word in the document,
'      then all spelling errors up to, but not including that error, are
found, and no
'      errors after that spelling error are found.

   System.Cursor = wdCursorWait
   Application.ScreenUpdating = False

   IncorrectResults  ' These tests produce incorrect results
   CorrectResults  ' These tests produce correct results

   Application.ScreenUpdating = False
   Set docNew = Nothing
   System.Cursor = wdCursorNormal
   Set yourErrors = Nothing
End Sub

Private Sub IncorrectResults()
   Dim rngError As Word.Range
   Debug.Print "Incorrect-0"
   CreateDocPunctuation
   ListSpellingErrors
   DeleteDoc

   Debug.Print "Incorrect-1"
   CreateDocPunctuation
   docNew.SpellingChecked = False
   ListSpellingErrors
   DeleteDoc

   Debug.Print "Incorrect-2"
   CreateDocPunctuation
   With docNew
       .SpellingChecked = False
       Set yourErrors = .SpellingErrors
   End With
   If yourErrors.Count = 0 Then
       Debug.Print "No spelling errors found."
   Else
       For Each rngError In yourErrors
           Debug.Print "From Spelling Errors: " & rngError.Start,
rngError.Text
       Next
   End If
   ListSpellingErrors
   DeleteDoc

   Debug.Print "Incorrect-3"
   CreateDocPunctuation
   docNew.SpellingChecked = True
   ListSpellingErrors
   DeleteDoc

   Debug.Print "Incorrect-4"
   CreateDocPunctuation
   Set yourErrors = docNew.SpellingErrors
   If yourErrors.Count = 0 Then
       Debug.Print "No spelling errors found."
   Else
       For Each rngError In yourErrors
           Debug.Print "From Spelling Errors: " & rngError.Start,
rngError.Text
       Next
   End If
   ListSpellingErrors
   DeleteDoc

   Debug.Print "Incorrect-5"
   CreateDocPunctuation
   docNew.SpellingChecked = False
   ListSpellingErrors
   DeleteDoc

   Debug.Print "Incorrect-6"
   CreateDocPunctuation
   Set yourErrors = docNew.SpellingErrors
   If yourErrors.Count = 0 Then
       Debug.Print "No spelling errors found."
   Else
       For Each rngError In yourErrors
           Debug.Print "From Spelling Errors: " & rngError.Start,
rngError.Text
       Next
   End If
   ListSpellingErrors
   DeleteDoc
End Sub

Private Sub CorrectResults()
   Dim rngError As Word.Range
   Debug.Print "Correct-0"
   CreateDocNoPunctuation
   ListSpellingErrors
   DeleteDoc

   Debug.Print "Correct-1"
   CreateDocNoPunctuation
   docNew.SpellingChecked = False
   ListSpellingErrors
   DeleteDoc

   Debug.Print "Correct-2"
   CreateDocNoPunctuation
   With docNew
       .SpellingChecked = False
       Set yourErrors = .SpellingErrors
   End With
   If yourErrors.Count = 0 Then
       Debug.Print "No spelling errors found."
   Else
       For Each rngError In yourErrors
           Debug.Print "From Spelling Errors: " & rngError.Start,
rngError.Text
       Next
   End If
   ListSpellingErrors
   DeleteDoc

   Debug.Print "Correct-3"
   CreateDocNoPunctuation
   docNew.SpellingChecked = True
   ListSpellingErrors
   DeleteDoc

   Debug.Print "Correct-4"
   CreateDocNoPunctuation
   Set yourErrors = docNew.SpellingErrors
   If yourErrors.Count = 0 Then
       Debug.Print "No spelling errors found."
   Else
       For Each rngError In yourErrors
           Debug.Print "From Spelling Errors: " & rngError.Start,
rngError.Text
       Next
   End If
   ListSpellingErrors
   DeleteDoc

   Debug.Print "Correct-5"
   CreateDocNoPunctuation
   docNew.SpellingChecked = False
   ListSpellingErrors
   DeleteDoc

   Debug.Print "Correct-6"
   CreateDocNoPunctuation
   Set yourErrors = docNew.SpellingErrors
   If yourErrors.Count = 0 Then
       Debug.Print "No spelling errors found."
   Else
       For Each rngError In yourErrors
           Debug.Print "From Spelling Errors: " & rngError.Start,
rngError.Text
       Next
   End If
   ListSpellingErrors
   DeleteDoc
End Sub

Private Sub CreateDocNoPunctuation()
   Set docNew = Documents.Add
   With docNew.Content
       .InsertAfter "kerect"
       .InsertParagraphAfter
       .InsertAfter "purrfect"
       .InsertParagraphAfter
       .InsertAfter "absolutivily"
   End With
End Sub

Private Sub CreateDocPunctuation()
   Set docNew = Documents.Add
   With docNew.Content
       .InsertAfter "kerect."
       .InsertParagraphAfter
       .InsertAfter "purrfect"
       .InsertParagraphAfter
       .InsertAfter "absolutivily"
   End With
End Sub

Private Sub CreateOriginalPunctuation() ' Currently not used
   Set docNew = Documents.Add
   With docNew.Content
       .InsertAfter "aQ"
       .InsertParagraphAfter
       .InsertAfter "Aq"
       .InsertParagraphAfter
       .InsertAfter "qA"
       .InsertParagraphAfter
       .InsertAfter "AQ"
       .InsertParagraphAfter
       .InsertParagraphAfter
       .InsertAfter "Option Explicit"
       .InsertParagraphAfter
       .InsertAfter "' Include the following in a document and run each of
the subs below"
       .InsertParagraphAfter
       .InsertAfter "' Hmmmmmmmmmmmm, the last word in this line is lower
case, iso."
       .InsertParagraphAfter
       .InsertParagraphAfter
       .InsertAfter "' Include the following in a document and run each of
the subs below."
       .InsertParagraphAfter
       .InsertAfter "' Hmmmmmmmmmmmm, the last word in this line is lower
case, iso."
       .InsertParagraphAfter
       .InsertParagraphAfter
       .InsertAfter "Private Sub CheckSpellingErrors()"
       .InsertParagraphAfter
      .InsertAfter " End Sub"
   End With
End Sub

Private Sub DeleteDoc()
   With docNew
       .Saved = True
       .Close
   End With
End Sub

Private Sub ListSpellingErrors()
   Dim rngError As Word.Range
   Dim lngLastStart As Long
   Dim strError As String

   docNew.Activate
           Selection.HomeKey unit:=wdStory, Extend:=wdMove
           lngLastStart = -1
           Do
               Set rngError = Selection.GoTo(what:=wdGoToSpellingError,
which:=wdGoToNext)
               With rngError
                   If .Start = lngLastStart Then
                       Exit Do
                   End If
                   strError = .Text
                   lngLastStart = .Start
                   .Select
                   Debug.Print .Start, .End, strError
               End With
           Loop

           Set rngError = docNew.Content
           lngLastStart = -1
           Do
               Set rngError = rngError.GoTo(what:=wdGoToSpellingError,
which:=wdGoToNext)
               With rngError
                   If .Start = lngLastStart Then
                       Exit Do
                   End If
                   strError = .Text
                   lngLastStart = .Start
                    Debug.Print .Start, .End, strError
               End With
           Loop

           Selection.HomeKey unit:=wdStory, Extend:=wdMove
           lngLastStart = -1
           Do
               Set rngError = Selection.GoToNext(what:=wdGoToSpellingError)
               With rngError
                   If .Start = lngLastStart Then
                       Exit Do
                   End If
                   strError = .Text
                   lngLastStart = .Start
                   .Select
                   Debug.Print .Start, .End, strError
               End With
           Loop

           Set rngError = docNew.Content
           lngLastStart = -1
           Do
               Set rngError = rngError.GoToNext(what:=wdGoToSpellingError)
               With rngError
                   If .Start = lngLastStart Then
                       Exit Do
                   End If
                   strError = .Text
                   lngLastStart = .Start
                    Debug.Print .Start, .End, strError
               End With
           Loop
   Set rngError = Nothing
End Sub
Howard Kaikow - 08 Apr 2005 11:49 GMT
Well, I've got to disappear for a while to prepare my annual mailing to my
Uncle Sam.

The only workaround I've found to the problem I posed is to move the
Selection/Range past the troublesome characters. Perhaps that's what Word
does internally anyway? I hate programming such hacks!

In any case, the method used to iterate the ProofReadingErrors makes a big
difference.

For a recent example:

Using For Each: 68280 milliseconds
Using For Next: 42469 milliseconds

Using GoToNext or GoTo with the Range and Selection objects used (I do not
recall which used what): 19826, 19950, 19857, and 19731 milliseconds.

So, you can see that it is much faster to traverse the document, instead of
using the ProofReadingErrors collection.

See ya in a week or so.

ta, ta!
Signature

http://www.standards.com/; See Howard Kaikow's web site.

 
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.