Please Note: This article is written for users of the following Microsoft Word versions: 2007, 2010, 2013, 2016, 2019, 2021, 2024, and Word in Microsoft 365. If you are using an earlier version (Word 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Word, click here: Determining the Length of a Non-Document Text File.

Determining the Length of a Non-Document Text File

Written by Allen Wyatt (last updated June 7, 2025)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, 2021, 2024, and Word in Microsoft 365


Several other WordTips have discussed opening, reading, writing, appending, and closing text files in your macros. Another command associated with sequential text files is the LOF function. If used on an open file, it returns the length of the file, in bytes. In other words, you can determine the number of characters in the file. This can come in handy if you are processing a text file character by character. You can determine the length of the file and then read that many characters before you finish processing the file.

The following code fragment is an example of how the LOF function is used:

Open "MyFile.Dat" for Input as #1
FileLen = LOF(1)

After the code is executed, the value in the FileLen variable will be the number of bytes in the MyFile.Dat file.

Note, again, that the file must be open to use the LOF function. (That's the purpose of the first line in the code fragment—to open the file.) If you don't want to open the file, then you can use the FileLen function to determine a file's length. This requires just a single code line:

FileLen = FileLen("MyFile.Dat")

Note that you enclose the filename in quote marks or use a string variable to contain the filename. You can also include a path in the filename parameter. If the file doesn't exist, then FileLen returns an error.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (11111) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, 2021, 2024, and Word in Microsoft 365. You can find a version of this tip for the older menu interface of Word here: Determining the Length of a Non-Document Text File.

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

Using RD Fields with Chapter Headings

The RD field can be handy for pulling together a bunch of documents into a single file. However, using the field can play ...

Discover More

Hidden Numbered Items Still Affect Numbering

Word allows you to make text hidden and then control whether the hidden text is displayed or printed. If your hidden text ...

Discover More

Discovering Printer Drift

How accurate is your printer when it comes to placing information on the printed page? The simple technique described in ...

Discover More

Do More in Less Time! An easy-to-understand guide to the more advanced features available in the Microsoft 365 version of Word. Enhance the quality of your documents and boost productivity in any field with this in-depth resource. Complete your Word-related tasks more efficiently as you unlock lesser-known tools and learn to quickly access the features you need. Check out Microsoft 365 Word For Professionals For Dummies today!

More WordTips (ribbon)

Complex Searches for Documents

When working with lots of documents, you may have need from time to time to discover which of those documents contain ...

Discover More

Saving Your Work Automatically

Word can be configured to save your work periodically; on any time schedule you desire. This tip explains this feature ...

Discover More

Inserting Only Part of a File

You can easily insert one document within another document. What you may not know is that you can limit which part of a ...

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}] (all 7 characters, in the sequence shown) 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 5 - 0?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.

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