PDA

View Full Version : Word Count VBA



bloodbath
09-07-2011, 07:20 AM
Ahoy!

I was wondering if someone could help with a VBA macro in Microsoft Word that counts the number of Characters

I need a character count macro. Here's what I need it to do.

for example;

This line

This text field will change all the time. (X characters)

I need a character count of "This text field will change all the time." in the "X" position after it. So, I would want it to read,

This text field will change all the time. (41 characters)

I would think that this would be easy if you know what you are doing, but I don't. can someone Help?

Talis
09-07-2011, 01:34 PM
You refer to "This line" in your example. I have assumed you want something to count the number of characters in a paragraph rather than "line" or "sentence".

Here are two routines: the first includes the paragraph mark in the count and the second excludes it.

Sub countChars1()
Dim numChar As Long
For Each oPara In ActiveDocument.Paragraphs
With oPara.Range
numChar = .Characters.Count
.MoveEnd Unit:=wdCharacter, Count:=-1
.InsertAfter Text:="(" & numChar & " characters)"
End With
Next
End Sub

Sub countChars2()
Dim numChar As Long
For Each oPara In ActiveDocument.Paragraphs
With oPara.Range
.MoveEnd Unit:=wdCharacter, Count:=-1
numChar = .Characters.Count
.InsertAfter Text:="(" & numChar & " characters)"
End With
Next
End Sub


Hope this is what you want. If not, post your requirements.

bloodbath
09-08-2011, 04:33 AM
Thanks a lot for your help Talis.

Here's what I'm trying to achieve;

In the attached file, I'd like for the macro to count the number of characters in the yellow coloured box and then return the number to the red coloured box.

Thanks again in advance.

Talis
09-08-2011, 12:18 PM
Sorry, can't help with this docx as I use Word 2003 and don't have Word 2007 or later.

Frosty
09-08-2011, 03:17 PM
Talis,

You can download the compatibility pack and still open a .docx document in Word 2003.

Bloodbath,

With the caveat that all (well, *most*, really) things are possible with some amount of programming... all you're showing in your attached template is some nested tables within a big table, and a couple of cells within that nested table formatted with a yellow background fill and yellow text (so that you have achieved the look of a yellow "box").

What Talis is showing you is how to count the characters within a paragraph.

The concept you're looking for is how to count characters in a range.

But the question remains... how are you going to identify the range? The count macro is simple. Too simple, as you can see... not even worthy of a separate function, until you give us more criteria.

Public Function fCharacterCount(rngWhere As Range) As Long
fCharacterCount = rngWhere.Characters.Count
End Function
As you can see (and Talis' code already displays), the .Characters.Count is an existing property. Talis is defining a range for you as a paragraph (which was as good a guess as any).

But what you have to define is what the range is... are you actually defining it by whatever text you have given a yellow background to? Whatever you've selected?

Do you want not to include specific characters in your count (i.e., paragraph marks, spaces, etc)? That could make the function return a little more robust, as you could do something along the following lines...

Public Function fCharacterCount(rngWhere As Range) As Long
Dim sTemp As String
sTemp = rngWhere.text
'now get rid of some characters
'a paragraph mark
sTemp = Replace(sTemp, vbCr, "")
'a blank space
sTemp = Replace(sTemp, " ", "")
'a question mark character
sTemp = Replace(sTemp, "?", "")

'return the "valid" character count
fCharacterCount = Len(sTemp)
'fCharacterCount = rngWhere.Characters.Count
End Function

However, all of this is dependent on how you are going to define your range.

If you are working in Word 2007/2010, you can more easily define your range using ContentControls (look these up).

But what you've shown is sort of nothing, at this point, from the programmer perspective. Because you have three nested tables, but the formatting on the three tables (and the cells containing yellow) is different in each case.

Here are my suggestions (in this order):
1. Look up Bookmarks
2. Look up Content Controls
3. Look up working with Ranges (this would be the programming part).

Then re-ask the question. Right now, there's nothing to really answer.

Frosty
09-08-2011, 03:23 PM
Oh, and the reason I'm being a stickler on your document, is that this is probably not, ultimately, the right way to approach the problem you're trying to solve.

Programmatically navigating through nested tables is tricky at best, and very breakable by simple end-user interaction (adding a cell at the wrong place).

The way you've created this table, the only way to reference the cells is by an exact cell number (which will change as the table changes).

Big picture, you'd probably want to set up some content controls which allow user input... and then you may be able to set up a field which takes care of the character count for you. But this is too open of an input to give more than general advice.

The more specific you are, the more specific we can be.

Talis
09-09-2011, 02:14 PM
Thanks Frosty,
I had Word 2007 on a desktop Dell but that machine has gone to Computer heaven.
The laptop I now work on crashed horribly when I tried to install SP3 (and yes, all my applications are legit) and consequently I decided not to install the compatibility pack some time ago.

bloodbath,
I would be interested to see the document in question if you are prepared to upload a .doc version.

Here's my effort to progress the VBA a little further.
If the text for which a word count is required has the insertion point in it then the word count will to be placed in a cell to its right.
As Frosty pointed out there is a need to identify particular cells to get a properly working program for you and you may be better of using other built-in means.
Sub countChars3()
Dim numChar As Long
Dim oRange As Range
Set oRange = Selection.Range
oRange.End = Selection.Cells(1).Range.End
oRange.Start = Selection.Cells(1).Range.Start
With oRange
.MoveEnd Unit:=wdCharacter, Count:=-1
numChar = .Characters.Count
End With
Selection.MoveRight Unit:=wdCell
Selection.InsertAfter "(" & numChar & " characters)"
End Sub

The line Selection.MoveRight Unit:=wdCell can be modified and added to with MoveUp, MoveDown, MoveRight lines of code, but that's somewhat feeble. Maybe you can live with it.