VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Functions For Reading and Writing Text Files

by Patrick Daniel (4 Submissions)
Category: Files/File Controls/Input/Output
Compatability: Visual Basic 3.0
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (8 Votes)

This code consists of 2 Function (ReadFile and WriteFile). All you have to do is Point ReadFile to a filepath and it will return the text within that file. Write file recieves a filepath and the string value to save to it. They include simple error handling..and can easily and quickly be advanced to handle open and save dialogs.

Rate Functions For Reading and Writing Text Files

'-- If you have any problems with this code please contact me
'-- at [email protected]. Feel free to drop me a line
'-- letting me know you are using this or if this code is
'-- helpfull to you. Enjoy!!
Public Function ReadFile(strPath As String) As Variant
On Error GoTo eHandler
  
  Dim iFileNumber As Integer
  Dim blnOpen As Boolean
  
  iFileNumber = FreeFile
  
  Open strPath For Input As #iFileNumber
  
  blnOpen = True
  
  ReadFile = Input(LOF(iFileNumber), iFileNumber)
  
eHandler:
  
  If blnOpen Then Close #iFileNumber
  
  If Err Then MsgBox Err.Description, vbOKOnly + vbExclamation, Err.Number & " - " & Err.Source
  
End Function
Public Function WriteFile(strPath As String, strValue As String) As Boolean
On Error GoTo eHandler
  Dim iFileNumber As Integer
  Dim blnOpen As Boolean
  
  iFileNumber = FreeFile
  
  Open strPath For Output As #iFileNumber
  
  blnOpen = True
  
  Print #iFileNumber, strValue
  
eHandler:
  
  If blnOpen Then Close #iFileNumber
  
  If Err Then
   MsgBox Err.Description, vbOKOnly + vbExclamation, Err.Number & " - " & Err.Source
  Else
   WriteFile = True
  End If
  
End Function

Download this snippet    Add to My Saved Code

Functions For Reading and Writing Text Files Comments

No comments have been posted about Functions For Reading and Writing Text Files. Why not be the first to post a comment about Functions For Reading and Writing Text Files.

Post your comment

Subject:
Message:
0/1000 characters