How did you
Open the files: with traditional file i/o statements or with the FileSystemObject? I didn't get any difficulties with either method using LOF, FileLen of Fso.FileSize. Note that for LOF the file
MUST be opened.
I submitted the following code:
Const c_strFileName As String = "D:\MainFolder\NormalFile.doc"
Public Sub Main()
Debug.Print vbCrLf & String(20, "-"
& vbCrLf & "Traditional File I/O Tests" & vbCrLf
Dim lngFileHnd As Long
lngFileHnd = FreeFile()
Open c_strFileName For Input Access Read As lngFileHnd
Debug.Print c_strFileName & " Size: " & LOF(lngFileHnd) & " bytes - " & _
"OPEN FILE - LOF"
Debug.Print c_strFileName & " Size: " & FileLen(c_strFileName) & " bytes - " & _
"OPEN File - FileLen"
Close lngFileHnd
'Following statement will cause an error, because the file is NOT open!
On Error GoTo PROC_ERROR
Debug.Print c_strFileName & " Size: " & LOF(lngFileHnd) & " bytes - " & _
"CLOSED File - LOF"
On Error GoTo 0
'Use FileLen for closed files
Debug.Print c_strFileName & " Size: " & FileLen(c_strFileName) & " bytes - " & _
"CLOSED File - FileLen"
'FSO methods and properties: File can be open or not
Debug.Print vbCrLf & String(20, "-"
& vbCrLf & "FSO Tests" & vbCrLf
Dim objFso As FileSystemObject
Dim objFile As File
Dim objTextStream As TextStream
Set objFso = New FileSystemObject
Set objFile = objFso.GetFile(c_strFileName)
Set objTextStream = objFs
penTextFile(objFile, ForReading)
Debug.Print objFile.Name & " Size: " & objFile.Size & " bytes - OPEN File"
objTextStream.Close
Debug.Print objFile.Name & " Size: " & objFile.Size & " bytes - CLOSED File"
Set objTextStream = Nothing
Set objFile = Nothing
Set objFso = Nothing
Debug.Print vbCrLf & String(20, "-"
PROC_EXIT:
Exit Sub
PROC_ERROR:
Debug.Print Err.Number & " " & Err.Description
Err.Clear
Resume Next
End Sub
and got the following results:
--------------------
Traditional File I/O Tests
a:\MainFolder\NormalFile.doc Size: 4608 bytes - OPEN FILE - LOF
a:\MainFolder\NormalFile.doc Size: 4608 bytes - OPEN File - FileLen
52 Bad file name or number
a:\MainFolder\NormalFile.doc Size: 4608 bytes - CLOSED File - FileLen
--------------------
FSO Tests
NormalFile.doc Size: 4608 bytes - OPEN File
NormalFile.doc Size: 4608 bytes - CLOSED File
--------------------
As you can observe, all results are consistent except for the unopened LOF case, where you get an error.
Sorry for the odd formatting: this editor has some (!° trouble with VB's continuation characters _________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]