Hi masters,
I try to write and read file into txt file but i dont know how to do it.
Anyone know how to achieve this?
Please help me.
any help will appreciated much.
Best Regards
Hi masters,
I try to write and read file into txt file but i dont know how to do it.
Anyone know how to achieve this?
Please help me.
any help will appreciated much.
Best Regards
A compact, practical follow-up for (building on 's quick example): the classic VB "Open/Print/Close" is fine for tiny scripts, but for safer, clearer code use the .NET System.IO APIs (if you are on VB.NET) or the FileSystemObject in VB6. Below are concise, ready-to-run patterns for writing, appending and reading without repeating the exact code already shown in the thread.
' VB.NET — write (overwrite) safely and with UTF-8
Using sw As New System.IO.StreamWriter("C:\Temp\Test.txt", False, System.Text.Encoding.UTF8)
sw.WriteLine("Line one from VB.NET")
sw.WriteLine("Line two")
End Using
' VB.NET — read whole file
Dim text As String = System.IO.File.ReadAllText("C:\Temp\Test.txt", System.Text.Encoding.UTF8)
' VB.NET — append a single line
System.IO.File.AppendAllText("C:\Temp\Test.txt", "Appended line" & Environment.NewLine, System.Text.Encoding.UTF8) ' VB6 — FileSystemObject approach (append and read)
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
' Append (creates file if missing)
Dim ts As Object
Set ts = fso.OpenTextFile("C:\Temp\Test.txt", 8, True) ' 8 = ForAppending
ts.WriteLine "Added from VB6"
ts.Close
' Read lines
Set ts = fso.OpenTextFile("C:\Temp\Test.txt", 1) ' 1 = ForReading
Do While Not ts.AtEndOfStream
Debug.Print ts.ReadLine
Loop
ts.Close
Set ts = Nothing
Set fso = Nothing Key tips and gotchas:
These patterns cover simple reads/writes and avoid common pitfalls (file locks, encoding, missing directories).
Jump to Post— Jx_Man 987See If This Helps :
An example of writing a file: Dim sFileText as String Dim iFileNo as Integer iFileNo = FreeFile 'open the file for writing Open "D:\Test.txt" For Output As #iFileNo 'please note, if this file already exists it will be overwritten! 'write some example …
See If This Helps :
An example of writing a file:
Dim sFileText as String
Dim iFileNo as Integer
iFileNo = FreeFile
'open the file for writing
Open "D:\Test.txt" For Output As #iFileNo
'please note, if this file already exists it will be overwritten!
'write some example text to the file
Print #iFileNo, "first line of text"
Print #iFileNo, " second line of text"
Print #iFileNo, "" 'blank line
Print #iFileNo, "some more text!"
'close the file (if you dont do this, you wont be able to open it again!)
Close #iFileNo We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.