1

我在尝试读取文本文件时遇到问题。基本上,文本文件由信息块组成,在每个块之间我有一个空白行。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

实际上我想从这些块中提取数字。但我收到此错误:这是我的代码:

  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
.....

你能帮我解决这个问题吗?太感谢了

4

1 回答 1

2

当您到达文件的最后一个 END 时,跳过或读取会将您带到文件的末尾。

我建议您,作为一般规则,如果您需要在移动之前跳过或阅读行以检查 EOF(对于文本文件或记录集)。您还可以使用带有条件的循环来测试是否未达到 EOF 或已找到所需的行。

您跳过行的第一个块可以写为

  strReadLine = ""
  do while not ts.AtEndOfStream and not (strReadLine LIKE "ST/*")
    strReadLine = ts.readline
  loop

再见

于 2015-01-20T09:23:14.730 回答