PDA

View Full Version : [SOLVED:] Count words / paragraphs in colored text



stevetalaga
12-29-2013, 02:50 AM
Hi all - I'm new here and pretty new to VBA as well so please bear with me.

I'm trying to write some code that will work its way through the active document and count the number of words and paragraphs for each color that is used in the document. I have managed to get a word count based on a specific font but can't find a way of counting based on colors or without specifying the color first. I would like the code to determine the color of the text (based on first character of the word just in case a word is made up of more than 1 color) and keep a counter for each color used.

The output I am looking for is something like:

Black 145
Red 26
Blue 34
etc. depending on which colors are used.

I have already searched the forum and Knowledge Base for posts containing Color and Count but no joy yet.

All help greatly appreciated.

Thanks
Steve

fumei
12-29-2013, 05:11 PM
So what do you get if there IS a word is made up of more than one color? BTW this would take quite a bit of processing. Just think...

Say you have the word "check". Say it starts with black "c", then black "h", black "e", black "c", and a red "k". Here is the list of separate instructions that would have to be performed.

Test for the color of "c".
STORE that value.
Test for the color of "h".
Test against the stored value of "c"
Test for the color of "e".
Test against the stored value of "c
Test for the color of "c".
Test against the stored value of first "c
Test for the color of "k".
Test against the stored value of first "c

That is just for ONE word (and a short one at that!).


count the number of words and paragraphs Both of this is possible, although you may be surprised what Word considers a "word". However you would have to explain the full logic of how to count them. If a paragraph has ten words in black, and one word in red, what does that mean as a count. ONE count as the paragraph is almost fully black? A nil count because it is not fully black? Only count the words, in which case what does a paragraph count actually mean?

fumei
12-29-2013, 05:13 PM
And what would you do if a word has more than two colors? THAT means you have to store a number of values for each and every word.

stevetalaga
12-29-2013, 07:41 PM
Thanks for your replies. Yes I see what you mean that it could get very messy. For both words and paragraphs I would just go with taking the color of the first character and counting the whole word or paragraph as that color.

Once I can get that working I may think about adding code to highlight words of multiple colors (for manual adjustments) but the important thing for now is to get a count of whole words and paragraphs in their 'starting' colors.

Thanks

fumei
12-29-2013, 10:18 PM
You need to stop putting words and paragraphs into the same logic. They can both be considered, but the logic has to be precise and clear. So please be clear as to what you want.

If the first character of the first word of say a 15 word paragraph is black, the entire paragraph is considered black, and gives a count of 1. Regardless of the number of words. Regardless of any change of color. In other words, words are irrelevant, and you are only counting paragraphs. So if the first character of the first word is black, but every other character is red, the paragraph is black, and counts for 1.

OR....

If the first character of the first word of say a 15 word paragraph is black, the entire paragraph is considered black, and gives a count of 15. Regardless of any change of color. In other words, paragraphs are irrelevant, and you are only counting words.

stevetalaga
12-29-2013, 11:27 PM
My apologies for being unclear.

I have 2 separate requirements by color: A word count and a paragraph count.

All of the time the whole paragraph should be the same color. If there is more than 1 color in a word or in a paragraph that is a coloring error and as such any "mis-count" can be justified by it - At this stage it is OK explain coloring glitches rather than capture and handle them all.

The person doing the coloring is the same person receiving the statistics and will be aware that:
1 - the color of paragraphs for the paragraph count is based on the first character of each paragraph
2 - the color of words for the word count is based on the first character of each word

So in your 2 examples:

"If the first character of the first word of say a 15 word paragraph is black, the entire paragraph is considered black, and gives a count of 1. Regardless of the number of words. Regardless of any change of color. In other words, words are irrelevant, and you are only counting paragraphs. So if the first character of the first word is black, but every other character is red, the paragraph is black, and counts for 1."
This is correct for the Paragraph count

And....

"If the first character of the first word of say a 15 word paragraph is black, the entire paragraph is considered black, and gives a count of 15. Regardless of any change of color. In other words, paragraphs are irrelevant, and you are only counting words."
This is not correct. If the first character of the first word was black and all other characters in the 15 word paragraph were red then the paragraph count would be 1 for Black and the word count would be 1 for Black and 14 for Red.

Hope I have explained this better this time and thanks for your time.

stevetalaga
01-01-2014, 04:25 AM
I think that maybe i am asking for too much here rather than just asking for pointers to get me going so that I can work on the overall solution myself.

To get me moving forward please could someone give me a clue how to test for the color of a word and then how to step through a document either character by character, word by word or paragraph by paragraph.

Thanks.

SamT
01-01-2014, 01:00 PM
Suggestions:

Dim MywordColors As Collection
Dim MyParaColors As Script(dictionary)


For Each Para in Document.Paragraphs
Select Case Para.Characters(1). Font.ColorIndex


For Each Wrd in Document.Words.Characters(1)
Select Case Wrd.Characters(1).Font.ColorIndex
Case wdAuto
MyWordColors("Auto") = MyWordColors("Auto") + 1 'There will be errors. See Colections or Dictionaries
Case wdBlack
Case wdBlue

fumei
01-01-2014, 05:43 PM
Dim oPara As Paragraph
Dim COL_PARA_auto As Long
Dim COL_PARA_Black As Long
Dim COL_PARA_Blue As Long
Dim COL_PARA_Red As Long
Dim COL_PARA_Yellow As Long
Dim COL_PARA_Green As Long
Dim COL_PARA_BrightGreen As Long
Dim COL_PARA_Teal As Long
Dim COL_PARA_Turquoise As Long
Dim COL_PARA_Violet As Long
Dim COL_PARA_White As Long
Dim COL_PARA_DarkBlue As Long
Dim COL_PARA_DarkRed As Long
Dim COL_PARA_DarkYellow As Long
Dim COL_PARA_Pink As Long

For Each oPara In ActiveDocument.Paragraphs
If Len(oPara.Range.Text) > 1 Then
Select Case oPara.Range.Characters(1).Font.ColorIndex
Case 0 ' auto
COL_PARA_auto = COL_PARA_auto + 1
Case 1 ' black
COL_PARA_Black = COL_PARA_Black + 1
Case 2 ' blue
COL_PARA_Blue = COL_PARA_Blue + 1
Case 3 ' turquoise
COL_PARA_Turquoise = COL_PARA_Turquoise + 1
Case 4 ' bright green
COL_PARA_BrightGreen = COL_PARA_BrightGreen + 1
Case 5 ' pink
COL_PARA_Pink = COL_PARA_Pink + 1
Case 6 ' red
COL_PARA_Red = COL_PARA_Red + 1
Case 7 ' yellow
COL_PARA_Yellow = COL_PARA_Yellow + 1
Case 8 ' white
COL_PARA_White = COL_PARA_White + 1
Case 9 ' dark blue
COL_PARA_DarkBlue = COL_PARA_DarkBlue + 1
Case 10 ' teal
COL_PARA_Teal = COL_PARA_Teal + 1
Case 11 ' green
COL_PARA_Green = COL_PARA_Green + 1
Case 12 ' violet
COL_PARA_Violet = COL_PARA_Violet + 1
Case 13 ' dark red
COL_PARA_DarkRed = COL_PARA_DarkRed + 1
Case 14 ' dark yellow
COL_PARA_DarkYellow 1
End Select
End If
Next
This gets the paragraph count.

fumei
01-01-2014, 10:23 PM
For Each aWord In oPara.Range.Words
Select Case aWord.Characters(1) And this gets the word counter

stevetalaga
01-02-2014, 01:19 AM
Guys - Thanks for your help. I will work through the code examples you provided tonight when I get home and do my best to understand it.

Thanks again

Steve