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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

write to a text file

Status
Not open for further replies.
Jun 9, 1999
45
HK
Hi,

How do I write something to a text file using the Code?
Please give me an example. Thanks.

Supernova
 
To use the FileSystemObject you'll have to set a reference to the 'Windows Script Host Object Model' using 'Tools --> References' in the IDE.

Code:
Public Sub WriteFile(ByVal strFile As String, ByVal strText As String)
On Error GoTo ErrHandler
  Dim fso As New FileSystemObject
  Dim ts As TextStream
  
  If Dir(strFile) = "" Then
    Set ts = fso.CreateTextFile(strFile)
    ts.Close
  End If
    
  Set ts = fso.OpenTextFile(strFile, ForAppending)
  
  ts.Write strText
  ts.Close
    
ExitHere:
  On Error Resume Next
  Set ts = Nothing
  Set fso = Nothing
  Exit Sub
ErrHandler:
  MsgBox "Error " & Err & " - " & Err.Description
  Resume ExitHere
End Sub
VBSlammer
redinvader3walking.gif

Unemployed in Houston, Texas
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top