source :from : http://stackoverflow.com/questions/18472596/write-a-line-in-a-txt-file
Sub WriteTextToFile()
Dim sFile As String, lFile As Long
sFile = "C:\Test.txt"
lFile = FreeFile
Open sFile For Append As lFile
Print #lFile, "first line of text"
Close lFile
End Sub
Update To pick the file from a File Open dialog
Sub WriteTextToFile()
Dim sFile As String, lFile As Long
sFile = Application.GetOpenFilename("*.txt,*.txt")
If sFile <> "False" Then
lFile = FreeFile
Open sFile For Append As lFile
Print #lFile, "first line of text"
Close lFile
End If
End Sub
In VB you can open files and then manipulate them using FileNum.
Now in a simple piece of code you could use Filenum 1 (one) manually
assigned in the fairly certain knowledge the number isn't being used but in
more complicated code this become very risky indeed because if you manually
assign the number you may start geting uppredictable results.
Hence FreeFile() which allocates the next 'unused' number. In the snippet
below Filenum is assigned to the next free number by FreeFile and thereafter
the file is referred to by the number assigned to it and not the name
沒有留言:
張貼留言