Please Note: This article is written for users of the following Microsoft Word versions: 97, 2000, 2002, and 2003. If you are using a later version (Word 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Word, click here: Determining the Length of a String.

Determining the Length of a String

by Allen Wyatt
(last updated February 18, 2012)

2

It is hard to imagine a function used more often with strings than the Len() function. This simple little function returns the length of any string. The following are a few examples that can work in your macros:

A = Len(MyString)
B = Len("This is a test")

The first line returns the length of the characters in the variable MyString. The second returns the number of characters between the quote marks (in this case, 14—remember that spaces count as characters).

If you want to determine the length of a selection, you follow a bit different approach:

C = Len(Selection)

This line returns the length of the current text selection in the document. Remember when calculating the length of a selection that paragraph marks (hard returns) count as two characters. That is because they are each really a carriage return followed by a line feed, even though all other macro commands treat them as a single character.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (777) applies to Microsoft Word 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Word (Word 2007 and later) here: Determining the Length of a String.

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

Cannot Convert a Document File to a Readable Document

Open a Word document file, and you can start to make edits and changes to your heart's content. But what happens if the file ...

Discover More

Shortcut to Move between Two Worksheets

Moving between to adjacent worksheets is easy; Excel provides a shortcut key to do the trick. If you want to move between two ...

Discover More

Incrementing Numeric Portions of Serial Numbers

If you use serial numbers that include both letters and numbers, you might wonder how you can increment the numeric portion ...

Discover More

Do More in Less Time! Are you ready to harness the full power of Word 2013 to create professional documents? In this comprehensive guide you'll learn the skills and techniques for efficiently building the documents you need for your professional and your personal life. Check out Word 2013 In Depth today!

More WordTips (menu)

Running Macros Based on Keywords

Wouldn't it be great if Word could execute a macro every time someone typed in a particular keyword or phrase? Word may not ...

Discover More

Counting the Instances of a Text String

Sometimes it is helpful to know how often a particular phrase appears within a document. If you need to know such a count, ...

Discover More

Temporarily Changing the Printer in a Macro

You can use a macro to print to any printer you have defined in Windows. It is good practice, if you are changing which ...

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 6Mpixels. 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 6 - 0?

2012-02-24 13:58:31

Steven Wells

Addendum to my previous comment:
Unlike the Len function, where a hard return counts as its two component characters, the built-in Word Count function ignores both hard and soft returns entirely, counting either as zero (0) characters.


2012-02-24 13:49:32

Steve Wells

This tip is okay for explaining the Len function, but why bother with that approach when there is already a much better built-in function in Word, though not obviously named.

Select the text that you want to consider, and on the Tools menu, click Word Count. A small box displays the statistics for your selection:
Pages
Words
Characters (no spaces)
Characters (with spaces)
Paragraphs
Lines

You also can select a check box to include footnotes and endnotes.

I use this feature often, so I have placed it onto one of my toolbars. To get the toolbar button:
1. Click Tools > Customize to open the Customize dialog box.
2. In the Commands tab under Categories, click Tools.
3. In the Commands box on the right, scroll to find Word Count and drag it to your toolbar.
4. Click Close.

If you look for the command under the Category: All Commands, the same item is available (alphabetically) as ToolsWordCount, along with a couple related commands, which place a list on the toolbar rather than in a dialog box. (Personally, I find the dialog box to be the more convenient interface.)


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.