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

Including a Printer's Name in a Footer

If you can produce output on a number of different printers, you may want Word to indicate on your printouts which printer ...

Discover More

Checking for Valid Hyperlinks

If you have a document containing a lot of hyperlinks, it would be cool if there was a way to check all those hyperlinks and ...

Discover More

Understanding the PDF/A Format

Word allows you to save your documents in PDF format so others can easily view them. You may not know, however, that Word ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More WordTips (menu)

Adding a Macro to a Toolbar

One of the easiest ways to quickly access a macro is to assign it to a toolbar button. How you make the assignment depends on ...

Discover More

Renaming a File

Need to rename a disk file from within a macro? You can do it using the Name command, described in this tip.

Discover More

Defining a Shortcut for a Macro

You can make running macros very easy if you assign a shortcut key to the macro. This tip demonstrates how easy it is to ...

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 0 + 7?

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.