How can I quickly load a text file into a string using VB6?
Asked
Active
Viewed 3.2k times
1
-
2possible 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 Answers
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
-
1Will 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
-
-
4@SetSailMedia not true, this is VB6. it requires a reference to `scrrun.dll` (`Microsoft Scripting Runtime`) – Brad Apr 29 '14 at 17:08
-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
-
-
1
-
1@CraigJ: No harm, until some bogus uninstaller unregisters the COM server. – wqw Dec 15 '11 at 14:49
-
@wqw: then you use a self-heal MSI installer for your app - problem solved. – CJ7 Dec 16 '11 at 03:05