|
-
May 6th, 2003, 03:37 AM
#1
Thread Starter
New Member
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
-
May 6th, 2003, 03:43 AM
#2
Hyperactive Member
filelist() has been dimensioned as an array but nothing has been put in it!!!!
-
May 6th, 2003, 03:46 AM
#3
I guess your Error occurs in this line
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!
-
May 6th, 2003, 04:21 AM
#4
Thread Starter
New Member
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.
-
May 6th, 2003, 04:39 AM
#5
Fanatic Member
VB Code:
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
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:
Private Sub Command1_Click()
Dim sFile As String
Dim sPath As String
Dim sFiles() As String
Dim nCounter As Long
Dim I As Long
sPath = "C:\"
sFile = Dir(sPath, vbNormal)
'-- Populates the Array
Do While Not sFile = ""
ReDim Preserve sFiles(nCounter) As String
sFiles(nCounter) = sFile
nCounter = nCounter + 1
sFile = Dir
Loop
'-- Prints array contents into console
For I = 0 To UBound(sFiles)
Debug.Print sFiles(I)
Next I
End Sub
-
May 6th, 2003, 05:13 AM
#6
Thread Starter
New Member
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.
-
May 6th, 2003, 06:09 AM
#7
Fanatic Member
its because your path must end with a \ , look at this new snippet for a fix
VB Code:
Private Sub Command1_Click()
Dim sFile As String
Dim sPath As String
Dim sFiles() As String
Dim nCounter As Long
Dim I As Long
sPath = "C:\windows"
sFile = Dir(sPath, vbNormal)
'-- Quick fix for Path
If Not Right$(sPath, 1) = "\" Then sPath = sPath & "\"
'-- Populates the Array
Do While Not sFile = ""
ReDim Preserve sFiles(nCounter) As String
sFiles(nCounter) = sFile
nCounter = nCounter + 1
sFile = Dir
Loop
'-- Prints array contents into console
For I = 0 To UBound(sFiles)
Debug.Print sFiles(I)
Next I
End Sub
-
May 6th, 2003, 06:12 AM
#8
Thread Starter
New Member
thanks eveybody I got the jist of it now, hope it didnt cause too much of a headache for you all
-
May 6th, 2003, 06:13 AM
#9
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|