Results 1 to 14 of 14

Thread: what is the problem in my code

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    131

    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

    VB Code:
    1. word = tmp(X)
    and if ii run it again for the same text and without ghanging in the code
    no errors appears


    why?
    why in the first run there is error, but second there is no error ?

  2. #2
    VB Guru ganeshmoorthy's Avatar
    Join Date
    Dec 2005
    Location
    Sharjah, United Arab Emirates
    Posts
    3,031

    Re: what is the problem in my code

    what is the value of X, when it runs and when it does not
    If an answer to your question has been helpful, then please, Rate it!

    Have done Projects in Access and Member management systems using BioMetric devices, Smart cards and BarCodes.


  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    131

    Re: what is the problem in my code

    yes i'm using Option Explicit

    see my code please

    VB Code:
    1. Sub insword()
    2.  
    3. Dim pos1 As Long, pos2 As Long, i As Integer, X As Integer
    4. Dim file As String, line As String, word As String
    5. Dim tmp() As String
    6.     file = vbNullString
    7.     i = 0
    8.     X = 0
    9.     Open "C:\newoutfile.txt" For Input As #1
    10.         tmp = Split(Input(LOF(1), 1), vbCrLf)
    11.     Close #1
    12.  
    13.  
    14.         Open "C:\outfile.txt" For Input As #1
    15.             Do While Not EOF(1)
    16.                 Line Input #1, line
    17.                     i = i + 1
    18.                     pos1 = InStr(1, line, "(")
    19.                     pos2 = InStr(1, line, ")")
    20.                         If pos1 > 0 And pos2 > 0 Then
    21.                             word = Mid(line, pos1 + 1, pos2 - pos1 - 1)
    22.                                If word <> "GLOSS" Then
    23.                                 word = tmp(X)
    24.                                     X = X + 1
    25.                                 End If
    26.                                line = Left(line, pos1 - 1) & "(" & word &
    27. ")" & Right(line, Len(line) - pos2)
    28.                                End If
    29.                             If i = 1 Then
    30.                                 file = line
    31.                             Else
    32.                                 file = file & vbCrLf & line
    33.                             End If
    34.             Loop
    35.         Close #1
    36.  
    37.             Open "C:\outfile.txt" For Output As #1
    38.                 Print #1, file
    39.             Close #1
    40.  
    41.  
    42. End Sub

  6. #6

  7. #7
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Jar, Norway
    Posts
    372

    Re: what is the problem in my code

    Nothing wrong, but it makes a strange text file in the output.
    What is the intention?

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    131

    Re: what is the problem in my code

    Hi MartinLiss...
    I attach file newoutfile.txt
    Attached Files Attached Files

  9. #9

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    131

    Re: what is the problem in my code

    This is the outfile.txt
    Attached Files Attached Files

  11. #11
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    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?

  12. #12
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: what is the problem in my code

    If your newoutfile.txt is blank then it will give the subscript error.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    131

    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:
    1. 'open command prompt to run Python script
    2. Open Apath & "dos.bat" For Output As #1
    3.     Print #1, "cd C:\python24"
    4.     Print #1, "buckwalter2unicode.py -i C:\buckwalter_morphan_1\data\newfile.txt -o C:newoutfile.txt -E mbcs"
    5.    ' Print #1, "buckwalter2unicode.py -i C:\buckwalter_morphan_1\data\newfile.txt -o C:newoutfile.txt  "
    6.    
    7.     Close #1
    8.  ' ShellAndWait Apath & "dos.bat"
    9.      Shell Apath & "dos.bat"


    i allways checking the newoutfile.txt it's always there "not empty"
    i don't understand what's the problem

  14. #14
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Jar, Norway
    Posts
    372

    Re: what is the problem in my code

    What is the value of X when you get the error?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width