Please Note: This article is written for users of the following Microsoft Word versions: 2007, 2010, 2013, 2016, 2019, 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 8, 2022)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365


1

Several other WordTips have discussed opening, reading, writing, appending, and closing text files. 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)

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, 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

Deleting MRU Entries

By default Word dutifully keeps track of the different files you've worked on in the past. If you want to delete an entry ...

Discover More

Duplexing Documents, by Default

If you have a printer that will print on both sides of a piece of paper, you may want to use that ability within Word. ...

Discover More

Setting a Default Date Format

Enter a date into a cell, and Excel allows you to format that date in a variety of ways. Don't see the date format you ...

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 (ribbon)

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

Getting Input from a Text File

VBA includes some commands that you can use to read information from text files (non-Word documents). These commands can ...

Discover More

Importing a Text File and Inserting after a Bookmark

Word macros are a great way to automate some of the ways in which you create documents. If you have a need to insert the ...

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 four minus 0?

2022-06-08 15:08:08

ron

Interesting. I'm not familiar with the LOF() command, so I looked it up. I found this
LOF(filenumber) – Length Open File
https://bettersolutions.com/vba/functions/lof-function.htm
Returns the length or size of an open file, in bytes (Long).
.
The file has to be open. You could also use the FILELEN() command without actually opening the file.
.
FILELEN(pathname)
https://bettersolutions.com/vba/functions/filelen-function.htm
Returns the length of a file in bytes (Long).
.
Seems FileLen() is simpler.


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.