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
Note:
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.
Learning Made Easy! Quickly teach yourself how to format, publish, and share your content using Word 2013. With Step by Step, you set the pace, building and practicing the skills you need, just when you need them! Check out Microsoft Word 2013 Step by Step today!
Need to know if the Num Lock key is on or off? You can use a short bit of macro code to figure out the state of the key.
Discover MoreYou can open multiple documents at the same time in Word, and each document occupies its own document window. Here's a ...
Discover MoreWhen creating macros, it is often necessary to know which directory is the default. Here's how you can find out by using ...
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
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)
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.
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2021 Sharon Parq Associates, Inc.
Comments