
How to count only the characters in red?
Hi, Lina,
This is a modification of the basic idea described at
http://www.mvps.org/word/FAQs/MacrosVBA/GetNoOfReplacements.htm. There's a
minor wrinkle that you're looking for formatting instead of a particular
word.
Sub CountRedChars()
Dim StartChars As Long, RedChars As Long
Dim oRg As Range
' find length of document
StartChars = ActiveDocument.Characters.Count
' temporarily remove all the red chars
Set oRg = ActiveDocument.Range
With oRg.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Font.Color = wdColorRed
.Replacement.Text = ""
.Format = True
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = False
.Execute Replace:=wdReplaceAll
End With
' find length of what's left
RedChars = StartChars - ActiveDocument.Characters.Count
' restore the doc if necessary
If RedChars > 0 Then
ActiveDocument.Undo
End If
' display the result
MsgBox "There are " & _
RedChars & _
" red characters."
End Sub
For those who may ask why I didn't make the Undo conditioned on whether
.Execute returns True, here's a bug: When you search for formatting only,
with .Text = "", you get a return of .Execute = False even though it found
and replaced something. :-(
--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://www.mvps.org/word
Quote:
> If you have a document and some of it is formatted in font
> red and other parts in font black, how to write a macro to
> count the number of characters that are black?
> P.S. Why do I need this... I use a template that contains
> lots of pre-typed text (formatted in red) and I create new
> documents on the basis of this template. I want to bill my
> clients only for the new text that I type into the
> document (formatted in black) and not for the text that is
> pre-typed (red). However, I can't simply count the
> characters in the template and subtract them from the
> total count of characters in the final document, as
> different parts of the pre-typed text are deleted with
> each new document). Thank you.