ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Writing multilines to a text file without closing (https://www.excelbanter.com/excel-programming/271263-writing-multilines-text-file-without-closing.html)

ccdubs

Writing multilines to a text file without closing
 
Hi,

I need to write figures to a text file. I have a loop that gets the updated
figures and then opens a text file and writes the figures, then closes the
text file and begins the process again. Is there a way that I can do this
without closing the text file each time the loop runs, i.e., make the macro
write to a new line.

Basically I am trying to decrease processor time.

The code that I have at the moment in my loop is as follows.

Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.OpenTextFile(FilePath, 8, 1)
a.WriteLine (Line1)
a.Close

Many thanks for any help.



Patrick Molloy[_3_]

Writing multilines to a text file without closing
 
the '8' in the line opening the text file tells Excel to
APPEND - so the code is doing partly what you want.
If you DIM 'a' and 'fs' as global, then you just need to
call one sub to initialise them, then you only need the
one line in the 'macro' to write the data. You'll need
another sub to close the file

Public FSO as FileSystemObject
Public txt as TextStream
Sub OpenTextStream()
SET FSO = New FilesystemObject
SET txt = FSO.OpenTextFile(Filename,ForAppend,False)
End Sub
Sub WriteToTextStream()
txt.WriteLine "Helloworld!"
End sub
Sub CloseTextStream()
txt.Close
SET txt = Nothing
SET FSO = Nothing
End Sub

HTH
Patrick Molloy
Microsoft Excel MVP

-----Original Message-----
Hi,

I need to write figures to a text file. I have a loop

that gets the updated
figures and then opens a text file and writes the

figures, then closes the
text file and begins the process again. Is there a way

that I can do this
without closing the text file each time the loop runs,

i.e., make the macro
write to a new line.

Basically I am trying to decrease processor time.

The code that I have at the moment in my loop is as

follows.

Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.OpenTextFile(FilePath, 8, 1)
a.WriteLine (Line1)
a.Close

Many thanks for any help.


.



All times are GMT +1. The time now is 08:38 PM.

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