We can write data into a file in two ways
The following piece of code demonstrates how to write data into a file.
Dim ObjFso
Dim StrFileName
Dim ObjFile
StrFileName = "C:\TestFile.txt"
Set ObjFso = CreateObject("Scripting.FileSystemObject")
'Creating a file for writing data
Set ObjFile = ObjFso.CreateTextFile(StrFileName)
'Writing a string into the file
ObjFile.Write("This is a sample string.")
ObjFile.Write("This string will be written continuosly after the first string.")
'Closing the file
ObjFile.Close
While writing data into the file this way, if you want to insert a new line character in between two strings you can do it by 'ObjFile.Write(VbCrLf)'. This will write a new line character into the file and what ever you write after the new line character will come in the next line.
Dim ObjFso
Dim StrFileName
Dim ObjFile
StrFileName = "C:\TestFile.txt"
Set ObjFso = CreateObject("Scripting.FileSystemObject")
'Creating a file for writing data
Set ObjFile = ObjFso.CreateTextFile(StrFileName)
'Writing a string into the file
ObjFile.WriteLine("This string will be in first line.")
ObjFile.WriteLine("This string will be in second line.")
'Closing the file
ObjFile.Close
Here the speciality of the 'WriteLine' function is that, after writing the string, it will automatically write a new line character also at the end of the string. So if you write a string again, it will come in the next line.
Dim ObjFso
Dim StrFileName
Dim ObjFile
StrFileName = "C:\TestFile.txt"
Set ObjFso = CreateObject("Scripting.FileSystemObject")
'Creating a file for writing data
Set ObjFile = ObjFso.CreateTextFile(StrFileName)
'Writing a string into the file
ObjFile.WriteLine("This string will be before the blank lines.")
'Writing 10 blank lines
ObjFile.WriteBlankLInes(10)
ObjFile.WriteLine("This string will be after the blank lines.")
'Closing the file
ObjFile.Close