我無法撰寫在 txt 檔案中查找單詞并回傳放置該單詞的行的代碼。
該詞通常位于第 31 行(位于字串的開頭),但有時位于下方。我試圖制作一個在第 32 行停止的回圈并且它起作用了,但是當字串放在不同的數字行上時,代碼不起作用。
那是因為我想到了尋找這個詞然后得到這條線。
我已經從很多地方嘗試了一些復制粘貼代碼,但我無法使它們作業。
這是 *.txt 檔案的示例:
%%%
VERSION:1
LANGUAGE:ENGLISH
%%%
MODULE RP02
PROC P02()
ConfL\Off;
ConfJ\Off;
vfeed.v_tcp := 20;
vfeed.v_ori := 300;
vrapid.v_tcp := 500;
vrapid.v_ori := 300;
vfeed.v_leax := 500;
vfeed.v_reax := 20;
vrapid.v_leax := 1000;
vrapid.v_reax := 20;
CurrZone.pzone_tcp :=1;
CurrZone.pzone_ori :=2;
CurrZone.pzone_eax := 100;
CurrZone.zone_leax := 30;
CurrZone.zone_reax := 30;
AccSet 10,50;
CurrWObj := caja1;
CurrWObj.oframe.trans := [0,0,0];
CurrWObj.oframe.rot := [1,0,0,0];
! Item ID 2.1: NCL Code - Carga herramienta_02_D63_L520_H12_CAR=1
LoadTool 2,H12_PAT_1,1,1;
! Item ID 2.2: NCL Code - Posicionamiento ST180 R
MoveAbsJ [[96.98,-14.64,15.98,87.92,-32.69,-87.52],[2040,0,0,0,0,0]],vrapid,CurrZone,CurrTool;
MoveAbsJ [[96.98,-14.64,15.98,87.92,-32.69,-87.52],[2540,0,0,0,0,0]],vrapid,CurrZone,CurrTool;
!---------------------------------------------
這是以前作業的代碼:
Dim fso As New FileSystemObject
Dim f As File
Dim fsoStream As TextStream
Dim strLine As String
Dim j As Integer
Set f = fso.GetFile("C:\fileToRead.txt")
Set fsoStream = f.OpenAsTextStream(ForReading)
j = 1
Do Until j > 32
strLine = fsoStream.ReadLine
Debug.Print strLine
j = j 1
Loop
但是當放置“LoadTool”的字串更改行號時,這不起作用......
I tried to modify copy/pastes I found on the web in order to achieve a FIND function or something similar and get the line number but I got stucked...
uj5u.com熱心網友回復:
我認為問題在于您停在第 32 行。只需運行回圈Until fsoStream.AtEndOfStream并檢查您的SearchWord. 如果找到則退出回圈。
Option Explicit
Sub Example()
Dim fso As New FileSystemObject
Dim f As Object
Set f = fso.GetFile("C:\fileToRead.txt")
Dim fsoStream As TextStream
Set fsoStream = f.OpenAsTextStream(ForReading)
Dim SearchWord As String
SearchWord = "LoadTool"
Dim strLine As String
Do Until fsoStream.AtEndOfStream
strLine = fsoStream.ReadLine
Debug.Print strLine
If Left$(strLine, Len(SearchWord)) = SearchWord Then
MsgBox "'" & SearchWord & "' was found."
Exit Do
End If
Loop
'strLine now contains
'LoadTool 2,H12_PAT_1,1,1;
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/346059.html
標籤:vba
下一篇:對陣列中的每一列進行簡單排序
