ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   excel97 vba to append lines to text file overwriting last 2 lines (https://www.excelbanter.com/excel-programming/315612-excel97-vba-append-lines-text-file-overwriting-last-2-lines.html)

Paul

excel97 vba to append lines to text file overwriting last 2 lines
 
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.



Stephen Bullen[_4_]

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




All times are GMT +1. The time now is 10:40 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com