Results 1 to 9 of 9

Thread: subscript out of range error

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2003
    Posts
    15

    subscript out of range error

    I am trying to put he content of a folder into an array, ut keep getting the same error message.

    Can anyone help



    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

    Private Sub Command1_Click()
    Dim n As Integer
    Dim fileList()

    For n = LBound(fileList()) To UBound(fileList())
    Debug.Print Str(n) & "" & fileList(n)
    Next n
    strMyFile = fileList(n)
    strMyFile = "C:\New Folder\"
    If Dir(strMyFile) = "" Then
    MsgBox "File " & strMyFile & " does not exist"
    Else
    PrintIt = ShellExecute(Me.hwnd, "PRINT", strMyFile, "", "", -1)
    End If
    End Sub

  2. #2
    Hyperactive Member
    Join Date
    Nov 1999
    Location
    Galway, Ireland
    Posts
    316
    filelist() has been dimensioned as an array but nothing has been put in it!!!!
    Slan

  3. #3
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863
    I guess your Error occurs in this line
    VB Code:
    1. strMyFile = fileList(n)
    n was used in the For Next Loop above, since the Loop is run thru n has now a value of UBound(fileList()) +1
    That should be it!
    Either use fileList(n-1) or fileList(UBound(fileList()))
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  4. #4

    Thread Starter
    New Member
    Join Date
    Mar 2003
    Posts
    15
    I tried both methods but cant seem to get rid of the error, is there a better way of attacking the problem of printing the folder content in one hit, without having to open each file.

  5. #5
    Fanatic Member Geespot's Avatar
    Join Date
    Oct 2001
    Location
    Birmingham, UK
    Posts
    577
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim n As Integer
    3. Dim fileList()
    4.  
    5. For n = LBound(fileList()) To UBound(fileList())
    6. Debug.Print Str(n) & "" & fileList(n)
    7. Next n
    8. strMyFile = fileList(n)
    9. strMyFile = "C:\New Folder\"
    10. If Dir(strMyFile) = "" Then
    11. MsgBox "File " & strMyFile & " does not exist"
    12. Else
    13. PrintIt = ShellExecute(Me.hwnd, "PRINT", strMyFile, "", "", -1)
    14. End If
    15. End Sub

    If you look carefully, your code does not make sense,
    1. It creates a dynamic array called fileList()
    2. You try to retrieve the contents of one element of the array without putting anything into it first. ex. strMyFile = fileList(n)
    3. You then directly under that line change the strMyFile variable without doing anything to it. ex. strMyFile = "C:\New Folder\"

    You want something like this..

    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim sFile As String
    3.     Dim sPath As String
    4.     Dim sFiles() As String
    5.     Dim nCounter As Long
    6.     Dim I As Long
    7.    
    8.     sPath = "C:\"
    9.     sFile = Dir(sPath, vbNormal)
    10.    
    11.     '-- Populates the Array
    12.     Do While Not sFile = ""
    13.         ReDim Preserve sFiles(nCounter) As String
    14.        
    15.         sFiles(nCounter) = sFile
    16.         nCounter = nCounter + 1
    17.        
    18.         sFile = Dir
    19.        
    20.     Loop
    21.    
    22.    
    23.     '-- Prints array contents into console
    24.     For I = 0 To UBound(sFiles)
    25.         Debug.Print sFiles(I)
    26.     Next I
    27.    
    28. End Sub

  6. #6

    Thread Starter
    New Member
    Join Date
    Mar 2003
    Posts
    15
    I Seee now i got it, at least i think i have.

    I have one more question, why is it that this will only work for the top level directory, if I try to add another sub directory (c:\level2) it goes back to subscritp out of range.

    Is this since the file attributes is vb Normal if so what do i need to change it to.

  7. #7
    Fanatic Member Geespot's Avatar
    Join Date
    Oct 2001
    Location
    Birmingham, UK
    Posts
    577
    its because your path must end with a \ , look at this new snippet for a fix

    VB Code:
    1. Private Sub Command1_Click()
    2.  
    3.     Dim sFile As String
    4.     Dim sPath As String
    5.     Dim sFiles() As String
    6.     Dim nCounter As Long
    7.     Dim I As Long
    8.    
    9.     sPath = "C:\windows"
    10.     sFile = Dir(sPath, vbNormal)
    11.    
    12.     '-- Quick fix for Path
    13.     If Not Right$(sPath, 1) = "\" Then sPath = sPath & "\"
    14.    
    15.     '-- Populates the Array
    16.     Do While Not sFile = ""
    17.         ReDim Preserve sFiles(nCounter) As String
    18.        
    19.         sFiles(nCounter) = sFile
    20.         nCounter = nCounter + 1
    21.        
    22.         sFile = Dir
    23.        
    24.     Loop
    25.    
    26.    
    27.     '-- Prints array contents into console
    28.     For I = 0 To UBound(sFiles)
    29.         Debug.Print sFiles(I)
    30.     Next I
    31.    
    32. End Sub

  8. #8

    Thread Starter
    New Member
    Join Date
    Mar 2003
    Posts
    15
    thanks eveybody I got the jist of it now, hope it didnt cause too much of a headache for you all

  9. #9
    Fanatic Member Geespot's Avatar
    Join Date
    Oct 2001
    Location
    Birmingham, UK
    Posts
    577
    Originally posted by Am_Musgrove
    thanks eveybody I got the jist of it now, hope it didnt cause too much of a headache for you all
    Its what the forums are for

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