Information about number of characters

M

Martin Müller

Dear experts,

is there any possibility to obtain the number of
characters contained within a selected string, word or
range?
The function should be similar to Selection.Information
(wdNumberOfPages) with Pages replaced by Characters. Also
obtaining the number of characters within a table's cell
would be helpful.
Thank you
Martin
 
J

Jay Freedman

Hi, Martin,

Assign the selected text to a string variable, and work with that.

As a "first draft", the character count would be

Len(Selection.Text)

but that would include all nonprinting characters (paragraph marks, tabs,
conditional hyphens, etc.). If you need to exclude those, you could use the
Replace function to get rid of them, something like this:

Dim strTemp As String
strTemp = Selection.Text
strTemp = Replace(strTemp, vbCr, "")
strTemp = Replace(strTemp, vbTab, "")
...
CharCount = Len(strTemp)

For a table cell, there are two characters that form the end-of-cell marker,
so you could do this:

CellChars = Len(Selection.Cells(1).Range.Text) - 2
 
K

Klaus Linke

Hi Martin,

You could also check out the ComputeStatistics method in the VBA help.

It corresponds to the information you are getting from "Tools > word count"
dialog. That is, you can get the character count with or without whitespace
characters (blanks, tabs); paragraph marks are never counted here.

Regards,
Klaus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top