|
-
Jun 4th, 2000, 08:00 AM
#1
Thread Starter
Lively Member
What code can I use to load all the text from a text file into a variable?
-
Jun 4th, 2000, 09:04 AM
#2
_______
try this...this is for one field in , delimited text file
' Open file for input.
Open "TESTFILE" For Input As #1
' Loop until end of file.
Do While Not EOF(1)
' Read data into two variables.
Input #1, MyString
' Print data to Debug window.
Debug.Print MyString
Loop
' Close file.
Close #1
"A myth is not the succession of individual images,
but an integerated meaningful entity,
reflecting a distinct aspect of the real world."
___ Adolf Jensen
-
Jun 4th, 2000, 11:32 AM
#3
Fanatic Member
If you want to load the entire text file into one single string, you might want this code:
Code:
Option Explicit
Function LoadFile(Filename As String) As String
'*** Dimension local variables...
Dim fileNum As Integer ' file number
Dim fileLen As Long ' file length
Dim fileData As String ' file data
fileNum = FreeFile ' get file handle
Open Filename For Binary As #fileNum ' open file
fileLen = LOF(fileNum) ' get file length
fileData = Space$(fileLen) ' set buffer size
Get #fileNum, , fileData ' read file data
Close #fileNum ' close file
LoadFile = fileData ' return value
End Function
Use the LoadFile function like this to load your AUTOEXEC.BAT for example:
Code:
Dim myFile As String
myFile = LoadFile("c:\autoexec.bat")
Notice that this function will load info from ANY file into the string variable, i.e. executable files, graphics files, text files, wave files, you name it.
Hope this helps.
-
Jun 4th, 2000, 12:19 PM
#4
New Member
Two methods I've used in the past...
Code:
Dim inFile&, lne$, text$
inFile = FreeFile
Open "c:\file.dat" for Input as inFile
Line Input #inFile, text
While Not EOF(inFile)
Line Input #inFile, lne
text = text + vbCrLf + lne
Wend
Close inFile
Code:
Dim inFile&, text$
inFile = FreeFile
Open "c:\file.dat" for Input as inFile
text = Input(LOF(inFile), #inFile)
Close inFile
Shab.
Code:
Print WeekDayName(vbMonday)
-
Jun 4th, 2000, 12:42 PM
#5
New Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|