Loading
Word.Tips.Net WordTips (Menu Interface)

Counting Characters in a Selection with VBA

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.

Related Tips:

The First and Last Word on Word! Bestselling For Dummies author Dan Gookin puts his usual fun and friendly candor back to work to show you how to navigate Word 2013. Spend more time working and less time trying to figure it all out! Check out Word 2013 For Dummies today!

 

Leave your own comment:

*Name:
Email:
  Notify me about new comments ONLY FOR THIS TIP
Notify me about new comments ANYWHERE ON THIS SITE
Hide my email address
*Text:
*What is 5+3 (To prevent automated submissions and spam.)
 
 
           Commenting Terms

Comments for this tip:

B    16 Apr 2015, 10:08
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
milkan    13 Jan 2015, 03:11
how to create online counter? http://www.wordcounter.biz/
Richard Kennedy    03 Dec 2014, 14:22
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
Richard Kennedy    03 Dec 2014, 14:13
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)
 
 

Our Company

Sharon Parq Associates, Inc.

About Tips.Net

Contact Us

 

Advertise with Us

Our Privacy Policy

Our Sites

Tips.Net

Beauty and Style

Cars

Cleaning

Cooking

DriveTips (Google Drive)

ExcelTips (Excel 97–2003)

ExcelTips (Excel 2007–2016)

Gardening

Health

Home Improvement

Money and Finances

Organizing

Pests and Bugs

Pets and Animals

WindowsTips (Microsoft Windows)

WordTips (Word 97–2003)

WordTips (Word 2007–2016)

Our Products

Helpful E-books

Newsletter Archives

 

Excel Products

Word Products

Our Authors

Author Index

Write for Tips.Net

Copyright © 2016 Sharon Parq Associates, Inc.