|
Spelling errors can be shown marked with a wavy
red underline in a Word document or hidden. Several users have
asked if there was a way to list all of the spelling errors in a
document. The macro that follows provides a means to list all
misspellings in a document sorted alphabetically or by frequency of the
misspelling. To use the macro you first need to create a Project
module and Class module. Open the Visual Basic Editor with
ALT+F11. Now with the View menu display the "Code" window,
"Project Explorer" window, and "Properties" window. Use the Insert
menu to create a new Project module and Class module. Rename Class
module "clsError." You can use any name you like for the Project module.
Here is a screen shot of my Project Explorer. I have created a
project module named "List Spelling Errors" and a Class module name
"clsError." |
|
Sub
SpellingErrorReportUsingClassModule()
Dim oError As clsError
Dim colErrors As Collection
Dim oSpErrors As ProofreadingErrors
Dim oSpError As Word.Range
Dim oSpErrorCnt As Integer
Dim uniqueSPErrors As Integer
Dim bolSortByFreq As Boolean
Dim j As Integer
Dim k As Integer
Dim l As Integer
Dim tempCount As Integer
Dim tempString As String
Dim oRng As Word.Range
Dim oTbl As Table
Set colErrors = New Collection
Set oSpErrors = ActiveDocument.Range.SpellingErrors
'Set sort order
bolSortByFreq = True
If MsgBox("The default sort order is error freqeuncy." _
& vbCr & "Do you want to sort errors" _
& " alphabetically instead?", vbYesNo) = vbYes Then
bolSortByFreq = False
End If
For Each oSpError In oSpErrors
On Error Resume Next
Set oError = colErrors(oSpError.Text)
On Error GoTo 0
If oError Is Nothing Then
Set oError = New clsError
oError.Name = oSpError.Text
colErrors.Add oError, oError.Name
End If
oError.Count = oError.Count + 1
Set oError = Nothing
Next
For j = 1 To colErrors.Count - 1
k = j
For l = j + 1 To colErrors.Count
If (Not bolSortByFreq And colErrors(l).Name <
colErrors(k).Name) _
Or (bolSortByFreq And
colErrors(l).Count > colErrors(k).Count) Then k = l
Next l
If k <> j Then
tempString = colErrors(j).Name
colErrors(j).Name = colErrors(k).Name
colErrors(k).Name = tempString
tempCount = colErrors(j).Count
colErrors(j).Count = colErrors(k).Count
colErrors(k).Count = tempCount
End If
Next j
'Display Results
oSpErrorCnt = ActiveDocument.Range.SpellingErrors.Count
uniqueSPErrors = colErrors.Count
Set oRng = ActiveDocument.Range
oRng.Move
oRng.InsertBreak wdSectionBreakNextPage
oRng.Select
Selection.ParagraphFormat.TabStops.ClearAll
With Selection
For Each oError In colErrors
.TypeText Text:=oError.Name & vbTab & oError.Count & vbCrLf
Next
End With
Selection.Sections(1).Range.Select
Selection.ConvertToTable
Selection.Collapse wdCollapseStart
Set oTbl = Selection.Tables(1)
oTbl.Rows.Add BeforeRow:=Selection.Rows(1)
oTbl.Cell(1, 1).Range.InsertBefore "Spelling Error"
oTbl.Cell(1, 2).Range.InsertBefore "Number of Occurrences"
oTbl.Columns(2).Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
Selection.Collapse wdCollapseStart
oTbl.Rows(1).Shading.BackgroundPatternColor = wdColorGray20
oTbl.Columns(1).PreferredWidth = InchesToPoints(4.75)
oTbl.Columns(2).PreferredWidth = InchesToPoints(1.9)
oTbl.Rows.Add
oTbl.Cell(oTbl.Rows.Count, 1).Range.InsertBefore "Summary"
oTbl.Cell(oTbl.Rows.Count, 2).Range.InsertBefore "Total"
oTbl.Rows(oTbl.Rows.Count).Shading.BackgroundPatternColor = wdColorGray20
oTbl.Rows.Add
oTbl.Cell(oTbl.Rows.Count, 1).Range.InsertBefore "Number of Unique
Misspellings"
oTbl.Cell(oTbl.Rows.Count, 2).Range.InsertBefore Trim(Str(uniqueSPErrors))
oTbl.Rows(oTbl.Rows.Count).Shading.BackgroundPatternColor = wdColorAutomatic
oTbl.Rows.Add
oTbl.Cell(oTbl.Rows.Count, 1).Range.InsertBefore "Total Number of Spelling
Errors"
oTbl.Cell(oTbl.Rows.Count, 2).Range.InsertBefore Trim(Str(oSpErrorCnt))
Selection.HomeKey wdStory
End Sub |