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

Adding Many No-Width Optional Breaks

One of the special characters you can add in a document is the no-width optional break. Although originally designed for use ...

Discover More

Maintaining Fields in a Merged Document

When merging documents, you may want to include some fields in the merged documents. For some fields this is impossible, as ...

Discover More

Weird Actions for Arrow Keys and Enter

If your arrow keys and the Enter key aren't working as you expect them to, the problem could have any number of causes. This ...

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)

Cleaning Up Text in a Macro

Need to remove extraneous characters from a text string? VBA makes it easy through the CleanString method, described in this ...

Discover More

Bypassing the Startup Macro

Word allows you to create a macro that is run automatically whenever the program is started. If you want to bypass the macro, ...

Discover More

Finding Long Sentences

For certain types of writing, you may want to make sure that the sentences in your document do not exceed a certain limit. ...

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?

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.