G
Greg Maxey
For those who frequent these NGs regularly you know that I can't spell.
Actually I can spell, but I am careless.
I have been monkeying around with some code that will search a document then
list alphabetically all misspelled words. I am using my budding but limited
knowledge of arrays to sort the list and purge out duplicates so words that
are misspelled more than once are only listed once. It works.
I was thinking it would be helpful to determine and list how many times each
word was misspelled. I think it would be possible, but I am not sure how I
would proceed.
Currently I am comparing each error to the contents of the existing array
and if a match occurs I am skipping that error. This way identical
misspellings the array only contains one instance of the misspelling. I
don't see how I could work a counter into this process.
Maybe if all words where put into the array initially, sorted in then
compare the first to the second and if a match occurs delete the first and
compare the second to the third until a match doesn't occur then jump up one
error and proceed on. This sounds achievable, but I don't know how to do
it.
Any thoughts?
Here is the current code:
Sub printSpellingErrors()
Dim arrSpArray() As String
Dim oSpErrors As ProofreadingErrors
Dim oSpError As Object
Dim i As Integer
Dim oRng As Range
Set oSpErrors = ActiveDocument.Range.SpellingErrors
If oSpErrors.Count = 0 Then
MsgBox "The document contains no spelling errors."
End
End If
ReDim arrSpArray(0)
'Add each error to the array if not a duplicate
For Each oSpError In oSpErrors
'Compare to each exist element in the array
For i = LBound(arrSpArray) To UBound(arrSpArray)
If oSpError = arrSpArray(i) Then
'Skip if already in array
GoTo SkipToNext
End If
Next i
'Otherwise add to array
arrSpArray(UBound(arrSpArray)) = oSpError
'Preserve and resize array for next element
ReDim Preserve arrSpArray(UBound(arrSpArray) + 1)
SkipToNext:
Next oSpError
'Remove last empty element
If UBound(arrSpArray) > 0 Then ReDim Preserve
arrSpArray(UBound(arrSpArray) - 1)
'Pass array to sort
BubbleSort arrSpArray
'Prepare for display
Set oRng = ActiveDocument.Range
oRng.Move
'oRng.Text = vbCr
oRng.InsertBreak wdSectionBreakNextPage
oRng.Move
oRng.Text = "List of Misspelled Words" & vbCr
oRng.Move
i = 0
For i = LBound(arrSpArray) To UBound(arrSpArray)
oRng.Text = arrSpArray(i) & vbCr
oRng.Collapse Direction:=wdCollapseEnd
Next i
'Clip empty paragraph
oRng.Characters.First.Previous.Delete
End Sub
Sub BubbleSort(TempArray As Variant)
Dim Temp As Variant
Dim i As Integer
Dim bolExchange As Integer
Do
bolExchange = False
'Loop through each element in the array.
For i = LBound(TempArray) To UBound(TempArray) - 1
'If element > next element then exchange the two elements.
If LCase(TempArray(i)) > LCase(TempArray(i + 1)) Then
bolExchange = True
Temp = TempArray(i)
TempArray(i) = TempArray(i + 1)
TempArray(i + 1) = Temp
End If
Next i
Loop While bolExchange
End Sub
Actually I can spell, but I am careless.
I have been monkeying around with some code that will search a document then
list alphabetically all misspelled words. I am using my budding but limited
knowledge of arrays to sort the list and purge out duplicates so words that
are misspelled more than once are only listed once. It works.
I was thinking it would be helpful to determine and list how many times each
word was misspelled. I think it would be possible, but I am not sure how I
would proceed.
Currently I am comparing each error to the contents of the existing array
and if a match occurs I am skipping that error. This way identical
misspellings the array only contains one instance of the misspelling. I
don't see how I could work a counter into this process.
Maybe if all words where put into the array initially, sorted in then
compare the first to the second and if a match occurs delete the first and
compare the second to the third until a match doesn't occur then jump up one
error and proceed on. This sounds achievable, but I don't know how to do
it.
Any thoughts?
Here is the current code:
Sub printSpellingErrors()
Dim arrSpArray() As String
Dim oSpErrors As ProofreadingErrors
Dim oSpError As Object
Dim i As Integer
Dim oRng As Range
Set oSpErrors = ActiveDocument.Range.SpellingErrors
If oSpErrors.Count = 0 Then
MsgBox "The document contains no spelling errors."
End
End If
ReDim arrSpArray(0)
'Add each error to the array if not a duplicate
For Each oSpError In oSpErrors
'Compare to each exist element in the array
For i = LBound(arrSpArray) To UBound(arrSpArray)
If oSpError = arrSpArray(i) Then
'Skip if already in array
GoTo SkipToNext
End If
Next i
'Otherwise add to array
arrSpArray(UBound(arrSpArray)) = oSpError
'Preserve and resize array for next element
ReDim Preserve arrSpArray(UBound(arrSpArray) + 1)
SkipToNext:
Next oSpError
'Remove last empty element
If UBound(arrSpArray) > 0 Then ReDim Preserve
arrSpArray(UBound(arrSpArray) - 1)
'Pass array to sort
BubbleSort arrSpArray
'Prepare for display
Set oRng = ActiveDocument.Range
oRng.Move
'oRng.Text = vbCr
oRng.InsertBreak wdSectionBreakNextPage
oRng.Move
oRng.Text = "List of Misspelled Words" & vbCr
oRng.Move
i = 0
For i = LBound(arrSpArray) To UBound(arrSpArray)
oRng.Text = arrSpArray(i) & vbCr
oRng.Collapse Direction:=wdCollapseEnd
Next i
'Clip empty paragraph
oRng.Characters.First.Previous.Delete
End Sub
Sub BubbleSort(TempArray As Variant)
Dim Temp As Variant
Dim i As Integer
Dim bolExchange As Integer
Do
bolExchange = False
'Loop through each element in the array.
For i = LBound(TempArray) To UBound(TempArray) - 1
'If element > next element then exchange the two elements.
If LCase(TempArray(i)) > LCase(TempArray(i + 1)) Then
bolExchange = True
Temp = TempArray(i)
TempArray(i) = TempArray(i + 1)
TempArray(i + 1) = Temp
End If
Next i
Loop While bolExchange
End Sub