SaveTextFile
From Visual Basic Wiki
A simple way to save a string to a textfile is:
Sub SaveTextfileA(Text As String, FileName As String)
Dim FileNumber As Integer
FileNumber = FreeFile
Open FileName For Output As #FileNumber
Print #FileNumber, Text;
Close #FileNumber
End Sub
To test it, you could use the following code:
Option Explicit
Private Sub Command1_Click()
SaveTextFileA "This text will be in a file!" & _
vbCrLf & "This is a second line!", _
"C:\test.txt"
End Sub
A major weakness of this function is that characters not present in the system codepage will not be saved properly, and will be replaced by question marks. So if you need to save textfiles containing such characters, you will need a Unicode version:
Sub SaveTextfileW(Text() As Byte, FileName As String)
Dim FileNumber As Integer, UnicodeMarker As Integer
UnicodeMarker = &HFEFF
FileNumber = FreeFile
Open FileName For Binary As #FileNumber
Put #FileNumber, , UnicodeMarker
Put #FileNumber, , Text
Close #FileNumber
End Sub
Example:
Option Explicit
Private Sub Command1_Click()
SaveTextFileW "This is a string containing Unicode characters:" & vbCrLf & _
ChrW(12373) & ChrW(12367) & ChrW(12425) & ChrW(24618) & _
ChrW(29539) & ChrW(12376) & ChrW(12419) & ChrW(12394) & _
ChrW(12356) & ChrW(12418) & ChrW(12435) & ChrW(-255), _
"C:\test.txt"
End Sub
