Many a times I feel I need to keep all information orderly even if I delete the file, I should have the data unformatted somewhere. I know it will be difficult and timeconsuming to index it or archive it and we simply delete it thinking that we wont need that information again only to find that we need that information urgently and we are not able to find it.
I think the following code can help a great lot of we run it against the event and store info in a specific folder as a backup and this will help us index and archive information in one folder regardless of whether we delete the file or not.
|
|
Sub PrintToTextFile()
Dim FileNum As Integer
Dim i As Long
Dim sPath, sInputLine As String
sPath = "C:\Documents and Settings\My Documents\"
FileNum = FreeFile ' Next free filenumber
Open sPath & "TextFile.txt" For Output As #FileNum
Open "C:\FOLDERNAME\TEXTFILE.TXT" For Append As #FileNum
sInputLine = "This line is written by a code"
Print #FileNum, sInputLine & " " & Time
Close #FileNum ' close the file
End Sub
The following code can be used to read the file.
|
|
Sub Read_TextFile()
Dim FileNum As Integer
Dim i As Long
Dim sPath, sInputLine As String
sPath = "C:\Documents and Settings\My Documents\"
Dim InputString As String
' Reading a text file line by line
' This example macro shows how you can read a textfile line by line:
FileNum = FreeFile 'next free filenumber
Open sPath & "TEXTFILE.TXT" For Input As #FileNum
While Not EOF(FileNum)
Line Input #FileNum, InputString ' read a line from the textfile
Debug.Print InputString ' do something with the string
'MsgBox InputString ' also you can use msgbox
Wend
Close #FileNum
End Sub
-Selva V Pasupathy
Contents of this Site « Selva’s Blog said
[...] Write / Read to a Text File [...]