excel97 vba to append lines to text file overwriting last 2 lines

  • Thread starter Thread starter Paul
  • Start date Start date
P

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top