ล่าสุดใช้ Code ตามนี้ครับ
แต่เจอ Error
Compile error : Cannot define a Public user-defined type within an object module
รบกวยช่วยดูให้ทีครับ
Code: Select all
Type Record
ID As Integer
Name As String * 20
End Type
Sub Collect_data_from_NC_code()
Dim myFile1 As Integer
Dim text As String
Dim buf As String
Dim buftext As String
Dim textline() As String
Dim iFile As Integer
iFile = FreeFile
Const TxtIn1 = "(K:"
Const TxtIn2 = "(B:"
Const TxtIn3 = "(S:"
Const TxtFin = "(ORIGIN 2 NOT USE)"
Const TxtFin2 = "(END OF FOOTER)"
Const TxtOut0 = "POPEN;"
Const TxtOut1 = "DPRNT[SR01*"
Const TxtOut2 = "DPRNT[SR02*"
Const TxtOut3 = "DPRNT[SR03*"
Const TxtOut4 = "DPRNT[SR04*START"
Const TxtOut5 = "DPRNT[SR05*FINISH"
Const TxtOut6 = "PCLOS;"
Const TxtOut7 = "DPRNT[SR05*CLEAR];"
Const TxtOut8 = "DPRNT[SR04*CLEAR];"
Dim K As String
Dim B As String
Dim S As String
Dim i As Double
Dim TxtSR01 As String
Dim TxtSR02 As String
Dim TxtSR03 As String
Dim TxtSR04 As String
Dim TxtSR05 As String
Dim X As Integer
Dim C As Integer
Dim J As Integer
Dim IngPosition As Long
For X = 9 To 38
'Open file
strFile_Path_In = Cells(X, 1)
strFile_Path_Out = Cells(X, 11)
'MsgBox (X)
'MsgBox (Cells(X, 2))
'strFile_Path_In = "D:\Mr.Jirawat\04_OA-support\02-MC\03_Other\Conprosys\Macro\Macro making\file1"
'strFile_Path_Out = "D:\Mr.Jirawat\04_OA-support\02-MC\03_Other\Conprosys\Macro\Macro making\fileout2"
'textline = Split(buf, vbLf)
Open strFile_Path_In For Input As #iFile
Dim MyRecord As Record
Open strFile_Path_In For Random As #1 Len = Len(MyRecord)
IngPosition = 20
Get #1, lngPosition, buf
Close #1
textline = Split(buf, vbLf)
For i = 0 To UBound(textline)
text = text & textline(i) & vbLf
If InStr(textline(i), TxtIn1) > 0 Then
MsgBox (textline(i))
TxtSR01 = Replace(textline(i), TxtIn1, "")
TxtSR01 = Replace(TxtSR01, " ", "")
TxtSR01 = Replace(TxtSR01, ")", "")
MsgBox (textline(i))
End If
If InStr(textline(i), TxtIn2) > 0 Then
MsgBox (textline(i))
TxtSR02 = Replace(textline(i), TxtIn2, "")
TxtSR02 = Replace(TxtSR02, " ", "")
TxtSR02 = Replace(TxtSR02, ")", "")
MsgBox (textline(i))
End If
If InStr(textline(i), TxtIn3) > 0 Then
MsgBox (textline(i))
TxtSR03 = Replace(textline(i), TxtIn3, "")
TxtSR03 = Replace(TxtSR03, " ", "")
TxtSR03 = Replace(TxtSR03, ")", "")
MsgBox (textline(i))
End If
If InStr(textline(i), TxtFin) > 0 Then
' Found line to insert
MsgBox (textline(i))
text = text & TxtOut0 & vbLf
text = text & TxtOut7 & vbLf
text = text & TxtOut1 & TxtSR01 & "];" & vbLf
text = text & TxtOut2 & TxtSR02 & "];" & vbLf
text = text & TxtOut3 & TxtSR03 & "];" & vbLf
text = text & TxtOut4 & "];" & vbLf
text = text & TxtOut6 & vbLf
End If
'If InStr(textline(i), TxtFin2) > 0 Then
' Found line to insert
'text = text & TxtOut0 & vbLf
'text = text & TxtOut8 & vbLf
'text = text & TxtOut1 & TxtSR01 & "];" & vbLf
'text = text & TxtOut2 & TxtSR02 & "];" & vbLf
'text = text & TxtOut3 & TxtSR03 & "];" & vbLf
'text = text & TxtOut5 & "];" & vbLf
'text = text & TxtOut6 & vbLf
'End If
'MsgBox (textline(1))
Next i
'MsgBox (text)
Open strFile_Path_Out For Output As #1
Print #1, text
MsgBox ("Endloop")
text = ""
Close #1
'Range("A1").Value = Mid(text, K + 3, 5)
'Range("A2").Value = Mid(text, B + 3, 2)
'Range("A3").Value = Mid(text, S + 3, 4)
Next X
End Sub