将下面一段用word保存为.rtf格式DIM LOCLD1203N= LOCATION OF POINT LD1203N UNITS=MM
AX NOMINAL MEAS +TOL -TOL DEV OUTTOL   
X -481.82 -481.48 1.50 -1.50 0.34 0.00 #
Y -534.08 -534.51 1.50 -1.50 -0.43 0.00 #
Z 88.00 88.76 1.50 -1.50 0.76 0.00 #
 
DIM LOCLD1204N= LOCATION OF POINT LD1204N UNITS=MM
AX NOMINAL MEAS +TOL -TOL DEV OUTTOL   
X -481.82 -483.76 1.50 -1.50 -1.94 0.44 <
Y -459.08 -459.81 1.50 -1.50 -0.73 0.00 #
Z 88.00 88.53 1.50 -1.50 0.53 0.00 #
 
DIM LOCLD1006N= LOCATION OF POINT LD1006N UNITS=MM
AX NOMINAL MEAS +TOL -TOL DEV OUTTOL   
X -503.00 -503.77 1.00 -1.00 -0.77 0.00 #
Y -497.00 -498.22 1.00 -1.00 -1.22 0.22 <
Z 105.00 105.38 1.00 -1.00 0.38 0.00 #
主要目的:
1.先一行一行的找到DIM开头的行
2.找到DIM空格后的=号前的字符串
用下面语句读取,为什么出现许多莫名其妙的字符串呢?Open FilePath For Input As #1
        Do While Not EOF(1)   ' 循环至文件尾。
            TextLine = ""
           Line Input #1, TextLine   ' 读入一行数据并将其赋予某变量。
           If InStr(1, TextLine, "DIM") <> 0 Then '找到带DIM的行
                m = InStr(1, TextLine, " ")
                n = InStr(1, TextLine, "=")
                Str = Mid(TextLine, m, n)
                MsgBox TextLine
                Exit Sub
           End If
        Loop
    Close #1

解决方案 »

  1.   

    文件格式的东西都在里面了,用txt就不会这样了
      

  2.   

    或者这样修改程序Private Sub Command1_Click()
        Dim TextLine As String
        Dim strT As String
        Dim m As Long
        Dim n As Long
        
        FilePath = "e:\test5.rtf"
        Open FilePath For Input As #1
        
        Do While Not EOF(1) ' 循环至文件尾。
            TextLine = ""
            Line Input #1, TextLine ' 读入一行数据并将其赋予某变量。
            
            If InStr(1, TextLine, "DIM") <> 0 Then '找到带DIM的行
                m = InStr(1, TextLine, "DIM ")
                n = InStr(1, TextLine, "=")
                strT = Mid(TextLine, m + 4, n - m - 4)
                Debug.Print TextLine
                Debug.Print strT
    '            Exit Sub
            End If
        Loop
        
        Close #1
    End Sub
      

  3.   

    谢谢,不过楼上的还是有问题,出现许多
    {\*\rsidtbl \rsid2561810}{\*\generator Microsoft Word 11.0.5604;}{\info{\title DIM LOCLD1203N= LOCATION OF POINT LD1203N UNITS=MM}{\author CJM}{\operator CJM}{\creatim\yr2011\mo4\dy8\hr7\min42}{\revtim\yr2011\mo4\dy8\hr7\min43}{\version1}{\edmins1}字符串
    是格式问题吗?
      

  4.   

    Private Sub Command1_Click()
        Dim TextLine As String
        Dim strT As String
        Dim m As Long
        Dim n As Long
        
        FilePath = "e:\test5.rtf"
        Open FilePath For Input As #1
        
        Do While Not EOF(1) ' 循环至文件尾。
            TextLine = ""
            Line Input #1, TextLine ' 读入一行数据并将其赋予某变量。
            
            If InStr(1, TextLine, "DIM") <> 0 Then '找到带DIM的行
                m = InStr(1, TextLine, "DIM ")
                n = InStr(1, TextLine, "=")
                strT = Mid(TextLine, m + 4, n - m - 4)
                'Debug.Print TextLine  '这个是你原来的数据,带了许多格式
                Debug.Print strT
    '            Exit Sub
            End If
        Loop
        
        Close #1
    End Sub