1

How can I quickly load a text file into a string using VB6?

CJ7
  • 22,579
  • 65
  • 193
  • 321
  • 2
    possible duplicate of [How can I read data from a text file using VB6?](http://stackoverflow.com/questions/2873830/how-can-i-read-data-from-a-text-file-using-vb6) – MarkJ Nov 21 '11 at 11:39

3 Answers3

13

This is the fastest way to load an entire file in VB6 without doing it line by line:

Function FileText (filename$) As String
    Dim handle As Integer
    handle = FreeFile
    Open filename$ For Input As #handle
    FileText = Input$(LOF(handle), handle)
    Close #handle
End Function
Authman Apatira
  • 3,994
  • 1
  • 26
  • 33
  • 1
    Will not work on all regional settings. Just use the code from the VB6 manual as in my answer to [this duplicate question](http://stackoverflow.com/questions/2873830/how-can-i-read-data-from-a-text-file-using-vb6/2875248#2875248) – MarkJ Nov 21 '11 at 11:41
5
Public Function ReadFileIntoString(strFilePath As String) As String

    Dim fso As New FileSystemObject
    Dim ts As TextStream

    Set ts = fso.OpenTextFile(strFilePath)
    ReadFileIntoString = ts.ReadAll

End Function 
CJ7
  • 22,579
  • 65
  • 193
  • 321
-2

Here's one way to do it using the filesystemobject:

Public Function ReadTextFileIntoString(strPathToFile as String) as String
  Dim objFSO As New FileSystemObject
  Dim objTxtStream As TextStream        
  Dim strOutput as String
  Set objTxtStream = objFSO.OpenTextFile(strPathToFile)
  Do until objTxtStream.AtEndOfStream
   strOutput = strOutput + objTxtStream.ReadLine
  Loop

  objTxtStream.Close
  ReadTextFileIntoString = strOutput
End Sub
Nonym
  • 6,199
  • 1
  • 25
  • 21