E
Edgar E. Cayce
OK Steve, here is the code for my counting - note that if I comment
out the .font tests it runs very fast, but slows back down to a crawl
if I do those tests.
"CountSet" is a structure used for my counts.
Sub CountChars(ByVal CurrentRange As Range, ByRef Counter As CountSet)
Dim BoldState As Boolean
Dim UnderlineState As Boolean
Dim ItalicState As Boolean
Dim LastFontName As String
Dim LastFontSize As Long
Dim LastFontColor As Long
Dim CharCounter As Long
Dim DocEnd As Long
BoldState = False
UnderlineState = False
ItalicState = False
LastFontName = CurrentRange.Characters(1).Font.Name
LastFontSize = CurrentRange.Characters(1).Font.Size
LastFontColor = CurrentRange.Characters(1).Font.Color
CharCounter = 0
With CurrentRange
DocEnd = .End - 1
.Collapse wdCollapseStart
Do While .Start < DocEnd
.MoveEnd
CharCounter = CharCounter + 1
If CharCounter Mod 1000 = 0 Then
Debug.Print CharCounter ' so I can see how fast it is
DoEvents
End If
If .Text = " " Then
Counter.Spaces = Counter.Spaces + 1
ElseIf .Text = vbTab Then
Counter.Tabs = Counter.Tabs + 1
ElseIf .Text = vbCr Then
Counter.Returns = Counter.Returns + 1
Else ' else it is a char, the only one where we care about
'bold, font, etc.
Counter.Chars = Counter.Chars + 1
If .Case = wdUpperCase Then
Counter.CapitalChars = Counter.CapitalChars + 1
End If
' note for these, we check transition as well as
'presence.
If .Font.Bold Then
Counter.BoldChars = Counter.BoldChars + 1
If BoldState = False Then
Counter.BoldTransitions = _
Counter.BoldTransitions + 1
End If
BoldState = True
Else
If BoldState = True Then
Counter.BoldTransitions = _
Counter.BoldTransitions + 1
End If
BoldState = False
End If
If .Font.Underline Then
Counter.UnderlineChars = _
Counter.UnderlineChars + 1
If UnderlineState = False Then
Counter.UnderlineTransitions = _
Counter.UnderlineTransitions + 1
End If
UnderlineState = True
Else
If UnderlineState = True Then
Counter.UnderlineTransitions = _
Counter.UnderlineTransitions + 1
End If
UnderlineState = False
End If
If .Font.Italic Then
Counter.ItalicChars = Counter.ItalicChars + 1
If ItalicState = False Then
Counter.ItalicTransitions = _
Counter.ItalicTransitions + 1
End If
ItalicState = True
Else
If ItalicState = True Then
Counter.ItalicTransitions = _
Counter.ItalicTransitions + 1
End If
ItalicState = False
End If
If .Font.Name <> LastFontName Then
Counter.FontTransitions = _
Counter.FontTransitions + 1
LastFontName = .Font.Name
End If
If .Font.Size <> LastFontSize Then
Counter.FontTransitions = _
Counter.FontTransitions + 1
LastFontSize = .Font.Size
End If
If .Font.Color <> LastFontColor Then
Counter.FontTransitions = _
Counter.FontTransitions + 1
LastFontColor = .Font.Color
End If
End If
.Collapse wdCollapseEnd
Loop
End With
End Sub