How to count only the characters in red? 
Author Message
 How to count only the characters in red?

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.


Sun, 21 Aug 2005 05:59:57 GMT  
 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.



Sun, 21 Aug 2005 06:38:23 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. running character count

2. Counting characters as they are typed

3. counting characters in a string

4. COUNT OF CHARACTERS IN A MAIL

5. How to count the Reference Characters ?

6. Count occurances of a character string?

7. Ignore strikethrough text in document character count

8. Scanning text & objDoc.Characters.Count

9. variable character count

10. Characters.Count vs. ComputeStatistics

11. Characters count without space in VB.NET

12. Characters and Line Count

 

 
Powered by phpBB® Forum Software