Problem with GoToNext(what:=wdGoToSpellingError)

H

Howard Kaikow

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
 
H

Howard Kaikow

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
 
H

Howard Kaikow

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!
 

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