Seatonian
04-15-2013, 01:21 AM
Hello, I am a raw beginner and this is my first post. I often have to correct students' assignments. I am constructing a macro to scan a one-page document and to (1) turn all the Spelling Errors red, (2) Collect the Spelling Errors and display in a table, along with (3) the FIRST Spelling Suggestion.
The macro fails at step (2): some Errors appear twice; some not at all. I can find no information on how the Collection of Errors is structured and MS Help - er - doesn't.
The Coding is shown below - very clunky. I'd also welcome any help in smartening it up a little.
Sub New_SpellCheck_EXPERIMENT()
' New_SpellCheck_EXPERIMENT Macro
' CALL with ALT + E (for Experiment)
Dim sugList As SpellingSuggestions
Dim sug As SpellingSuggestion ' Could delete all instances of this variable ????
Dim strSugList As String
Dim Blunder, mistake As Variant
Dim eCount As Integer 'May be deleted later.
Dim oRow As Row 'Delete these two Table variables on amalgamation.
Dim oCell As Cell
For Each Blunder In ActiveDocument.SpellingErrors
Blunder.Font.Color = wdColorRed
eCount = eCount + 1
Next Blunder
MsgBox "There are " & eCount & " Errors in this text."
ActiveDocument.Select
With Selection
.EndKey unit:=wdStory
.Collapse 0
.TypeParagraph
.TypeParagraph
End With
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=eCount + 3, _
NumColumns:=5, DefaultTableBehavior:=wdWord9TableBehavior, _
AutoFitBehavior:=AutofitContent
With Selection.Tables(1)
If Style <> "Table Grid" Then
Style = "Table Grid"
End If
.AutoFormat Format:=wdTableFormatGrid3
End With
With Selection
.Tables(1).Rows.Alignment = wdAlignRowCenter
With .Tables(1)
.Range.Font.Size = 14
'Code below populates Column Headers in Row 1.
.Cell(1, 1).Range.Text = " ERROR "
.Cell(1, 2).Range.Text = " CORRECTION "
.Cell(1, 3).Range.Text = " WRITE HERE "
.Cell(1, 4).Range.Text = " WRITE HERE "
.Cell(1, 5).Range.Text = " WRITE HERE "
End With
End With
ActiveDocument.ActiveWindow.View.TableGridlines = True
MsgBox "Is this table grid OK? "
MsgBox "eCount is still = " & eCount
All of the above code works perfectly
The for each next loop below does not catch all errors. Every second error is missing, but the loop iterates the correct number of times, displaying some errors & their corrections twice in the table.
eCount = 0
For Each mistake In ActiveDocument.SpellingErrors
Set sugList = GetSpellingSuggestions(Word:=mistake)
If sugList.Count = 0 Then
strSugList = "No suggestion"
Else
strSugList = sugList(1) 'Only the first suggestion is needed.
End If
eCount = eCount + 1
'Column 2 is printed before Col.1 to prevent GetSpellingSuggestions
'from reading Col.1 and REALLY fouling up the table.
With ActiveDocument.Tables(1)
.Cell(eCount + 1, 2).Range.Select
Selection.TypeText strSugList
Selection.Collapse 0
.Cell(eCount + 1, 1).Range.Select
Selection.TypeText mistake
Selection.Collapse 0
End With
'MsgBox "eCount is = " & eCount & " after this iteration."
Next mistake
End Sub
The macro fails at step (2): some Errors appear twice; some not at all. I can find no information on how the Collection of Errors is structured and MS Help - er - doesn't.
The Coding is shown below - very clunky. I'd also welcome any help in smartening it up a little.
Sub New_SpellCheck_EXPERIMENT()
' New_SpellCheck_EXPERIMENT Macro
' CALL with ALT + E (for Experiment)
Dim sugList As SpellingSuggestions
Dim sug As SpellingSuggestion ' Could delete all instances of this variable ????
Dim strSugList As String
Dim Blunder, mistake As Variant
Dim eCount As Integer 'May be deleted later.
Dim oRow As Row 'Delete these two Table variables on amalgamation.
Dim oCell As Cell
For Each Blunder In ActiveDocument.SpellingErrors
Blunder.Font.Color = wdColorRed
eCount = eCount + 1
Next Blunder
MsgBox "There are " & eCount & " Errors in this text."
ActiveDocument.Select
With Selection
.EndKey unit:=wdStory
.Collapse 0
.TypeParagraph
.TypeParagraph
End With
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=eCount + 3, _
NumColumns:=5, DefaultTableBehavior:=wdWord9TableBehavior, _
AutoFitBehavior:=AutofitContent
With Selection.Tables(1)
If Style <> "Table Grid" Then
Style = "Table Grid"
End If
.AutoFormat Format:=wdTableFormatGrid3
End With
With Selection
.Tables(1).Rows.Alignment = wdAlignRowCenter
With .Tables(1)
.Range.Font.Size = 14
'Code below populates Column Headers in Row 1.
.Cell(1, 1).Range.Text = " ERROR "
.Cell(1, 2).Range.Text = " CORRECTION "
.Cell(1, 3).Range.Text = " WRITE HERE "
.Cell(1, 4).Range.Text = " WRITE HERE "
.Cell(1, 5).Range.Text = " WRITE HERE "
End With
End With
ActiveDocument.ActiveWindow.View.TableGridlines = True
MsgBox "Is this table grid OK? "
MsgBox "eCount is still = " & eCount
All of the above code works perfectly
The for each next loop below does not catch all errors. Every second error is missing, but the loop iterates the correct number of times, displaying some errors & their corrections twice in the table.
eCount = 0
For Each mistake In ActiveDocument.SpellingErrors
Set sugList = GetSpellingSuggestions(Word:=mistake)
If sugList.Count = 0 Then
strSugList = "No suggestion"
Else
strSugList = sugList(1) 'Only the first suggestion is needed.
End If
eCount = eCount + 1
'Column 2 is printed before Col.1 to prevent GetSpellingSuggestions
'from reading Col.1 and REALLY fouling up the table.
With ActiveDocument.Tables(1)
.Cell(eCount + 1, 2).Range.Select
Selection.TypeText strSugList
Selection.Collapse 0
.Cell(eCount + 1, 1).Range.Select
Selection.TypeText mistake
Selection.Collapse 0
End With
'MsgBox "eCount is = " & eCount & " after this iteration."
Next mistake
End Sub