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: Using Seek In a Macro.
by Allen Wyatt
(last updated January 28, 2012)
Several other tips in other issues of WordTips discuss opening, reading, writing, appending, and closing text files. Another macro command associated with sequential text files is the Seek command. If used on an open file, it positions the internal file pointer at a specific character number in the file. The following code fragment is an example of how it is used:
Open "DOSTEXT.DAT" for Input as #1 iFileLen = LOF(1) Seek 1, iFileLen / 2
These program lines use the LOF function to determine the length of the file. The last line then positions the internal file pointer half way through the file. All subsequent reading or writing of the file will take place from that position.
You can also use Seek as a function to determine your current position within a text file. This is what this code does:
iCurPos = Seek(1)
This command leaves the internal file pointer where it was, but sets iCurPos to a value representing how many characters into the file the pointer is. The iCurPos value is the position at which all subsequent reading and writing of the file will take place.
WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (1385) 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: Using Seek In a Macro.
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!
Think you might have a corrupt document? There is no easy way to tell if this is the case, but there are some things you can ...Discover More
Save a document for the first time, and Word helpfully suggests a filename you can use or change. If you want this suggested ...Discover More
Afraid of messing up an existing document by some changes you are considering? Consider opening the document as read-only, so ...Discover More
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."