G
Greg Maxey
For the most part, Class modules are still a mystery to me. What
little knowledge I have of them was imparted by Jezebel when I was
working on a project to list, count and sort spelling errors.
Earlier today and OP wanted to simply list all spelling errors
contained in a document in a separate document. I thought of my
earlier project and passed on the link.
I then started thinking about a simpler code to do exactly what the OP
requested "and" prevent duplicates. Here is the result of those
thoughts:
Sub NoClass()
Dim DupChk As Object
Dim oCol As Collection
Dim oSpError As Word.Range
Dim i As Long
Set oCol = New Collection
'Add to collection
For Each oSpError In ActiveDocument.Range.SpellingErrors
On Error Resume Next
Set DupChk = oCol(oSpError.Text)
On Error GoTo 0
If DupChk Is Nothing Then
oCol.Add oSpError
End If
Set DupChk = Nothing
Next
'Publish results
Documents.Add
For i = 1 To oCol.Count
ActiveDocument.Range.InsertAfter oCol(i) & vbCr
Next i
End Sub
That seems straightforward and does the job.
To do the same thing with a class, I first inserted a class module,
named it clsError and pasted in this code:
Private mName As String
Public Property Get Name() As String
Name = mName
End Property
Public Property Let Name(NewValue As String)
mName = NewValue
End Property
In the project module, I used this code:
Sub HighClass()
Dim oError As clsError
Dim oCol As Collection
Dim oSpError As Word.Range
Set oCol = New Collection
'Add to collection
For Each oSpError In ActiveDocument.Range.SpellingErrors
On Error Resume Next
Set oError = oCol(oSpError.Text)
On Error GoTo 0
If oError Is Nothing Then
Set oError = New clsError
oError.Name = oSpError.Text
oCol.Add oError, oError.Name
End If
Set oError = Nothing
Next
'Publish results
Documents.Add
With ActiveDocument.Range
For Each oError In oCol
.InsertAfter oError.Name & vbCr
Next
End With
End Sub
This works equally as well.
What again is the benefit of using a Class? Is there any benefit in
this example?
Thanks.
little knowledge I have of them was imparted by Jezebel when I was
working on a project to list, count and sort spelling errors.
Earlier today and OP wanted to simply list all spelling errors
contained in a document in a separate document. I thought of my
earlier project and passed on the link.
I then started thinking about a simpler code to do exactly what the OP
requested "and" prevent duplicates. Here is the result of those
thoughts:
Sub NoClass()
Dim DupChk As Object
Dim oCol As Collection
Dim oSpError As Word.Range
Dim i As Long
Set oCol = New Collection
'Add to collection
For Each oSpError In ActiveDocument.Range.SpellingErrors
On Error Resume Next
Set DupChk = oCol(oSpError.Text)
On Error GoTo 0
If DupChk Is Nothing Then
oCol.Add oSpError
End If
Set DupChk = Nothing
Next
'Publish results
Documents.Add
For i = 1 To oCol.Count
ActiveDocument.Range.InsertAfter oCol(i) & vbCr
Next i
End Sub
That seems straightforward and does the job.
To do the same thing with a class, I first inserted a class module,
named it clsError and pasted in this code:
Private mName As String
Public Property Get Name() As String
Name = mName
End Property
Public Property Let Name(NewValue As String)
mName = NewValue
End Property
In the project module, I used this code:
Sub HighClass()
Dim oError As clsError
Dim oCol As Collection
Dim oSpError As Word.Range
Set oCol = New Collection
'Add to collection
For Each oSpError In ActiveDocument.Range.SpellingErrors
On Error Resume Next
Set oError = oCol(oSpError.Text)
On Error GoTo 0
If oError Is Nothing Then
Set oError = New clsError
oError.Name = oSpError.Text
oCol.Add oError, oError.Name
End If
Set oError = Nothing
Next
'Publish results
Documents.Add
With ActiveDocument.Range
For Each oError In oCol
.InsertAfter oError.Name & vbCr
Next
End With
End Sub
This works equally as well.
What again is the benefit of using a Class? Is there any benefit in
this example?
Thanks.