List Spelling Errors

Home Up Odds & Ends Photo Gallery Search Contact Me

 

 

The information in this website is provided without risk or obligation and free of charge.  However, if you have benefitted from my efforts here and would like to make a contribution to help me continue and maintain this work then any donation will be greatly appreciated. Please click the adjacent button to access PayPal.  Thank you.
 

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."

Next copy the following code into the Class module:

Option Explicit
Private mName As String
Private mCount As Long
Public Property Get Name() As String
  Name = mName
End Property
Public Property Let Name(NewValue As String)
  mName = NewValue
End Property
Public Property Get Count() As Long
  Count = mCount
End Property
Public Property Let Count(NewValue As Long)
  mCount = NewValue
End Property

A special note of thanks to regular Word Newsgroup contributor "Jezebel" for introducing me to using Class modules.

Now copy the following code into the Project module:

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

Here is a small sample of text with some rather obvious spelling errors. 

When you run the macro a message box is displayed asking you if you want to display the results in frequency order (default) or alphabetically.

The result is displayed in a table appended section at the end of the document.  Here is a screen shot of the results generated for the sample text above:

Need help applying macros?  See fellow MVP Graham Mayor's  Guide for Installing Macros 
Here is a link to a downloadable zip file containing a template with the code provided above.  You can use this template as a Word Addin for creating Spelling Error Reports in documents:  Spelling Error Report

Looking for something else?

Google