Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to create and write to a text file

Status
Not open for further replies.

sourabhjha

Programmer
Jan 13, 2002
121
0
0
IN
Can someone give me a sample code for creating and writing to a text file using vb.
Thanks in advance
-Sourabh
 
Writes the entire file from a single string, CrLF s already included
Public Sub WriteProto(strFilename As String, _
strOut As String)
Dim lngFile As Long
Dim strResponse As String
Dim strWhile As String
lngFile = FreeFile()
strWhile = "Opening"
On Error Resume Next
Open strFilename For Output As lngFile
strResponse = Err.Description
On Error GoTo 0
If Len(strResponse) = 0 Then
strWhile = "Writing"
On Error Resume Next
Print #lngFile, strOut; ' No Crlf added at end
On Error GoTo 0
End If
On Error Resume Next
Close lngFile
On Error GoTo 0
If Len(strResponse) > 0 Then
Err.Raise vbObjectError + 513, , strResponse
End If
End Sub
Generate Forms/Controls Resizing/Tabbing Class
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
So much easier using FSO:

Add a reference under Projects to the "Microsoft Scripting Runtime"

Dim fso As New Scripting.FileSystemObject
Dim f As Scripting.TextStream

fso.CreateTextFile sPATHandNAME, overwrite:=True
Set f = fso_OpenTextFile(sPATHandNAME, IOMode:=ForAppending, Create:=False)

f.WriteLine "My Line 1"
f.WriteLine "My Line 2"

f.Close

Set f = Nothing
Set fso = Nothing
 
Thanks for the FSO code!

I wanted to eliminate those pesky quotation marks. That did the trick :)
 
Or

-----------------
Dim f as byte

f=freefile
Open sPATHandNAME for OUTPUT as #f
Print #f, "My Line 1"
Print #f, "My Line 2"
Close #f
-----------------
Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top