Counting Characters in a Selection with VBA

by Allen Wyatt
(last updated July 9, 2015)

4

When you are developing macros, it is often helpful to know how many characters are in a text selection. The following VBA macro, CharCount, will display the number of characters in a selection.

Sub CharCount()
    Dim Title As String
    Dim CharCount As Integer
    Dim Message As String

    Title = "CharCount"
    CharCount = Len(Selection)
    Message = LTrim(Str(CharCount)) + " character"
    If CharCount <> 1 Then Message = Message + "s"
    MsgBox Message, vbOKOnly, Title
End Sub

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (746) applies to Microsoft Word 97, 2000, 2002, and 2003.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Protecting Bookmarks

Bookmarks are a great boon in developing and working with documents—until someone deletes them. When it comes to ...

Discover More

Adding Page Numbers

Ever want to add page numbers to your document? Word allows you to control many aspects of page numbering. Here's how to add ...

Discover More

Converting Field Results to Text

Fields are meant to be dynamic, providing a result based on conditions at the time they are updated. You may want to convert ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More WordTips (menu)

Turning Off Screen Updating

When working with macros, you can often speed up processing by turning off the updating of the screen. Best news is that it ...

Discover More

Trimming Spaces from Strings

When processing text with a macro, you often need to remove extraneous spaces from the text. VBA provides three handy ...

Discover More

Determining the Current Directory

When creating macros, it is often necessary to know which directory is the default. Here's how you can find out by using the ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 8Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is three more than 5?

2015-04-16 10:08:17

B

For i = 1 To wordDocument.Paragraphs.Count
' Get the first 20 characters of the paragraph
paragraphText = wordDocument.Paragraphs(i).Range.Text
If Len(paragraphText) > 20 Then
paragraphText = Left(paragraphText, 20)
End If
' qne put it in the next cell down
ActiveCell.Offset(i - 1, 0) = paragraphText
Next i


2015-01-13 03:11:31

milkan

how to create online counter? http://www.wordcounter.biz/


2014-12-03 14:22:19

Richard Kennedy

Addendum

Len(Selection.Range) returns 0 when no characters are selected.

When characters are selected Len(Selection.Range) returns the same value as Len(Selection), Selection.Characters.Count and Selection.Range.Characters.Count


2014-12-03 14:13:19

Richard Kennedy

You can also obtain character counts using the ComputeStatistics method of the Range object:

CharCount = Selection.Range.ComputeStatistics(wdStatisticCharactersWithSpaces)

(spaces are included in the count, but not carriage returns)

Selection.Range.ComputeStatistics(wdStatisticCharacters)

(spaces are not included in the count, punctuation marks are included)

Len(Selection), Selection.Characters.Count, and
Selection.Range.Characters.Count return Selection.Range.ComputeStatistics(wdStatisticCharactersWithSpaces)
plus carriage return characters.

If no characters are selected, then
Selection.Range.ComputeStatistics(wdStatisticCharactersWithSpaces) = 0 and
Selection.Range.ComputeStatistics(wdStatisticCharacters) = 0, but Len(Selection) = 1, Selection.Characters.Count = 1, and Selection.Range.Characters.Count = 1.

(tested with Word 2013)


This Site

Got a version of Word that uses the menu interface (Word 97, Word 2000, Word 2002, or Word 2003)? This site is for you! If you use a later version of Word, visit our WordTips site focusing on the ribbon interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.