View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Stephen Bullen[_4_] Stephen Bullen[_4_] is offline
external usenet poster
 
Posts: 205
Default excel97 vba to append lines to text file overwriting last 2 lines

Hi Paul,

Hi, I want to mod some vba in excel to append new lines of text to a .txt
file but not from the end of the file but from 2 lines back. The last two
lines are always of the form <word1 <word2 and will be overwritten by the
appended text. Any pointers will be much appreciated as I am out on a limb
away from any reference material.


VBA's Open For Append only ever writes to the end of a text file. To overwrite
some lines, I think the only thing you'll be able to do is to write it as a new
file:

- Open the original file for reading
- Open a new file for writing
- Scan through the original file, writing each line to the new file until the
<word1 is found
- Write the new lines to the new file
- Close both files
- Delete the original file and rename the new one over it.

Regards

Stephen Bullen
Microsoft MVP - Excel
www.oaltd.co.uk