what is the problem in my code
i wrote this code to insert word from (newoutfile.txt ) which have a list of words
between brackets in (outfile.txt)
the problem is when i run it an error displyed in the first run
(subscript out of range ) pointing to this line
and if ii run it again for the same text and without ghanging in the code
no errors appears :confused:
why?
why in the first run there is error, but second there is no error ? :sick:
Re: what is the problem in my code
what is the value of X, when it runs and when it does not
Re: what is the problem in my code
subscript out of range is when your index value is greater then the bounds of the array or collection. So I would assume that you have a value in x that is either too large or your array is not dimensioned correctly.
Re: what is the problem in my code
Also how are tmp and X defined, and are you using Option Explicit?
Re: what is the problem in my code
yes i'm using Option Explicit
see my code please
VB Code:
Sub insword()
Dim pos1 As Long, pos2 As Long, i As Integer, X As Integer
Dim file As String, line As String, word As String
Dim tmp() As String
file = vbNullString
i = 0
X = 0
Open "C:\newoutfile.txt" For Input As #1
tmp = Split(Input(LOF(1), 1), vbCrLf)
Close #1
Open "C:\outfile.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, line
i = i + 1
pos1 = InStr(1, line, "(")
pos2 = InStr(1, line, ")")
If pos1 > 0 And pos2 > 0 Then
word = Mid(line, pos1 + 1, pos2 - pos1 - 1)
If word <> "GLOSS" Then
word = tmp(X)
X = X + 1
End If
line = Left(line, pos1 - 1) & "(" & word &
")" & Right(line, Len(line) - pos2)
End If
If i = 1 Then
file = line
Else
file = file & vbCrLf & line
End If
Loop
Close #1
Open "C:\outfile.txt" For Output As #1
Print #1, file
Close #1
End Sub
Re: what is the problem in my code
Could you also put newoutfile.txt in a zip file and attach it?
Re: what is the problem in my code
Nothing wrong, but it makes a strange text file in the output.
What is the intention?
1 Attachment(s)
Re: what is the problem in my code
Hi MartinLiss...
I attach file newoutfile.txt
Re: what is the problem in my code
I'm sorry but I also need outfile.txt
1 Attachment(s)
Re: what is the problem in my code
Re: what is the problem in my code
There may be something else going on in your program because I can run the code you showed the first time (and any number of times) without any problems. Do you do something before you run the insword Sub?
Re: what is the problem in my code
If your newoutfile.txt is blank then it will give the subscript error.
Re: what is the problem in my code
i'm using a shell command befor this function
actually newoutfile.txt is the output of this shell command
this is the code
VB Code:
'open command prompt to run Python script
Open Apath & "dos.bat" For Output As #1
Print #1, "cd C:\python24"
Print #1, "buckwalter2unicode.py -i C:\buckwalter_morphan_1\data\newfile.txt -o C:newoutfile.txt -E mbcs"
' Print #1, "buckwalter2unicode.py -i C:\buckwalter_morphan_1\data\newfile.txt -o C:newoutfile.txt "
Close #1
' ShellAndWait Apath & "dos.bat"
Shell Apath & "dos.bat"
i allways checking the newoutfile.txt it's always there "not empty" :confused:
i don't understand what's the problem
Re: what is the problem in my code
What is the value of X when you get the error?