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