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

Read-Only Documents

Using both Word and Windows, there are a variety of ways you can mark a file as read-only so that it cannot be changed. This ...

Discover More

Specifying an Axis Scale in Microsoft Graph

Microsoft Graph is a handy way to add charts to your document if you don't have access to Excel. Here's how to adjust the ...

Discover More

Changing Label Sizes

Information formatted for one type of label may someday need to be printed on a different type of label. Here's some ...

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)

Changing ToolTips for a Macro Button

Want to change the ToolTip that appears when you hover the mouse over a button on a toolbar? It's a bit more involved than ...

Discover More

Creating a Command List

Want a list of all the commands available in Word? You can get one easily by following these steps.

Discover More

Extracting INCLUDEPICTURE File Names

If you use the INCLUDEPICTURE field to add images to your document, you may love the macro in this tip. It allows you to pull ...

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 for this tip:

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. 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 3 - 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.

Links and Sharing