http://msdn.microsoft.com/library/en-us/dnofftalk/html/office09072000.asp
Working with Files, Folders and Drives: More VBA Tips and Tricks by David
Shank
http://support.microsoft.com/default.aspx?scid=kb;en-us;151262
Working with Sequential Access Files
http://www.applecore99.com/gen/gen029.asp
Reading and Writing
http://msdn2.microsoft.com/en-us/library/czxefwt8.aspx
----------- Sample code to read a file --------------
Sub ReadStraightTextFile()
Dim sStr as String
Dim LineofText As String
Dim rw as Long
rw = 0
Open "C:\FILEIO\TEXTFILE.TXT" For Input As #1
sStr = ""
Do While Not EOF(1)
Line Input #1, LineofText
sStr = sStr & lineofText
if len(sStr) >= 178 then
rw = rw + 1
cells(rw,1).Value = sStr
sStr = ""
End if
Loop
'Close the file
if len(sStr) > 0 then
cells(rw,1).Value = sStr
End if
Close #1
End Sub
------- Some code by Jake Marx --------
function to truncate a file at a given test string (including the test string)
Private Const mlTEMP_FOLDER As Long = 2
Public Function gbTruncateFile(rsFullPath As String, _
rsTruncString As String) As Boolean
Dim fso As Object
Dim tsSrc As Object
Dim tsDest As Object
Dim bDone As Boolean
Dim sTemp As String
Dim lTruncPos As Long
Dim sDestPath As String
On Error GoTo ErrHandler
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(rsFullPath) Then
Set tsSrc = fs
penTextFile(rsFullPath)
sDestPath = fso.GetSpecialFolder(mlTEMP_FOLDER) _
& Application.PathSeparator & fso.GetFileName(rsFullPath)
Set tsDest = fso.CreateTextFile(sDestPath, True)
With tsSrc
Do While Not (.AtEndOfStream Or bDone)
sTemp = .ReadLine
lTruncPos = InStr(1, sTemp, rsTruncString, _
vbTextCompare)
If lTruncPos Then
sTemp = Left$(sTemp, lTruncPos - 1)
bDone = True
End If
tsDest.WriteLine sTemp
Loop
End With
tsSrc.Close
tsDest.Close
fso.CopyFile sDestPath, rsFullPath, True
gbTruncateFile = True
End If
ExitRoutine:
On Error Resume Next
tsSrc.Close
tsDest.Close
Kill sDestPath
Set tsSrc = Nothing
Set tsDest = Nothing
Set fso = Nothing
Exit Function
ErrHandler:
Resume ExitRoutine
End Function
-------------------------------------
Sample code if you wanted to read the entire file into a variable:
Sub fdsa()
Dim FileNumber As Integer, FilePath As String
Dim FullString As String
FilePath = "C:\Export.txt"
FileNumber = FreeFile
Open FilePath For Input As #FileNumber
FileLength = LOF(FileNumber)
Do While Not EOF(FileNumber)
Line Input #FileNumber, FullString
MsgBox FullString, vbInformation + vbOKOnly
Loop
Close #FileNumber
End Sub
You should be able to coble something together.