vba 如何修复运行时错误 62 ''输入文件末尾'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16769837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to fix Run time error 62 ''Input past end of file'
提问by Kaja
I have a problem while trying to read a text file. Basically, the text file is comprised of blocks of information, between each block I have a blanck row. Hier is a sample of my text:
我在尝试读取文本文件时遇到问题。基本上,文本文件由信息块组成,每个块之间有一个空白行。Hier 是我的文本示例:
FESTWERT FRAUS
LANGNAME "bla bla bla"
FUNKTION dfgg
EINHEIT_W "ü"
WERT -9.2654122070312500
END
KENNFELD KFDWNWCSA 4 4
LANGNAME "bla bla bla"
FUNKTION FGHK
EINHEIT_X "8/kl"
EINHEIT_Y "bla"
EINHEIT_W "bla"
ST/X 1658.0000000000000000 987.0000000000000000 3698.0000000000000000 3520.0000000000000000
ST/Y -30.0000000000000000
WERT 22.0000000000000000 16.9870000000000000 10.3210000000000000 10.0000000000000000
ST/Y 0.0000000000000000
WERT 10.0000000000000000 10.0000000000000000 10.0000000000000000 10.0000000000000000
ST/Y 45.2500000000000000
WERT 10.0000000000000000 10.0000000000000000 10.0000000000000000 10.0000000000000000
ST/Y 21.0000000000000000
WERT 22.0000000000000000 16.0000000000000000 10.0000000000000000 10.0000000000000000
END
actually I want to extract the numbers from these blocks. but I get this error: this is my Code:
实际上我想从这些块中提取数字。但我收到此错误:这是我的代码:
Dim fso As New FileSystemObject
Dim ts As TextStream
Set ts = fso.OpenTextFile(Name, ForReading)
Do While Not ts.AtEndOfStream
strArray = Split(ts.ReadLine, Space(1), 2) ' Extrahieren, was in einer Zeile ist
If Len(Join(strArray, "")) <> 0 Then
If strArray(0) = "KENNFELD" Then
SWKNF = True
ts.SkipLine
ts.SkipLine
ts.SkipLine
ts.SkipLine
ts.SkipLine
wertkenfeld = strArray(1)
strArray(1) = ""
End If
If strArray(0) = "END" Then werden
If SWKNFL = True Then
For P = 0 To X - 1
DoCmd.RunSQL ("INSERT INTO Test_DCML_G (XValue,Wert,name) VALUES ('" & Stx(P) & "','" & wert(P) & "','" & wertkenfeld & "');")
Next P
End If
SWKNF = False
SWKNFL = False
Erase Warray
X = 0
W = 0
Erase Yarray
Erase Xarray
Erase Stx
Erase wert
ts.SkipLine ' I get the error in this line
End If
.....
Would you please help me to solve this problem? Thank you so much
你能帮我解决这个问题吗?非常感谢
回答by Wiz
When you reach the last END of the file, a skip or read takes you beyond then end of the file.
当您到达文件的最后一个 END 时,跳过或阅读会将您带到文件末尾。
I suggest you, as a general rule, if you need to skip or read lines to check EOF before moving (both for text files or recordset). You can also use a loop with a condition in which you test if EOF has not been reached or the desired line has been found.
我建议您,作为一般规则,如果您需要在移动之前跳过或阅读行以检查 EOF(文本文件或记录集)。您还可以使用带有条件的循环来测试是否未达到 EOF 或是否已找到所需的行。
The first chunk in which you skip lines could be written as
跳过行的第一个块可以写为
strReadLine = ""
do while not ts.AtEndOfStream and not (strReadLine LIKE "ST/*")
strReadLine = ts.readline
loop
Bye
再见