yamiusingvba
Technical User
I am currently working on a database that uses the Windows API to call an open dialog box to allow the user to select an Excel file, which I then import into the database. Currently, the user has to select one file at a time (which is working perfectly); however, they may receive anywhere from 8 to 32 files each month that need to be uploaded.
The open dialog box I use is adapted from the faq on tek-tips:
tek-tips.com/faqs.cfm?fid=2484
I modified the code I use to call the dialog box to set the flags cdlOFNExplorer (to use long file names) and cdlOFNAllowMultiselect (to allow multiple selections).
My understanding is that if long file names are used, then the code will return a single string containing the path and file names delimited by a vbNull.
To test the result of selecting multiple files I used this code (which if I understand it correctly, should split the returned string into the path and individual file names.
(Note: rather than defining my own sep variable, I have also tried using vbNull and vbNullChar as the delimiter, with identical results)
When I run the code, if I select one file, I get the intended result in the immediate window:
C:\Documents and Settings\xxxxxxxxxx\Desktop\filename1.xls
C:\Documents and Settings\xxxxxxxxxx\Desktop\filename2.xls
However, if I select multiple files, I get the following result in the immediate window:
C:\Documents and Settings\xxxxxxxxxx\Desktop
C:\Documents and Settings\xxxxxxxxxx\Desktop
It does not matter if I select two, three, or ten files; the Debug.Print only runs twice, and it only returns the path with no file names.
I also tried saving the whole string returned into a single string variable, and the using MsgBox "variableName" to test the code; however, if I select multiple files, this only returns the path as well. Additionally, I modified the code above as follows:
The result in the immediate window should be:
C:\Documents and Settings\xxxxxxxxxx\Desktop
filename1.xls
filename2.xls
However, what I get is an out of bounds error when I reach strFiles(1). It appears that if one file is selected the code works properly, but if more than one file is selected the code only returns the path and not the file names.
How can I get the code to return both the path and the file name for each file selected, and assign the individual file names and paths to a collection, array, or individual variables that I can then reference in the rest of my code?
Any help you can provide would be greatly appreciated...
The open dialog box I use is adapted from the faq on tek-tips:
tek-tips.com/faqs.cfm?fid=2484
I modified the code I use to call the dialog box to set the flags cdlOFNExplorer (to use long file names) and cdlOFNAllowMultiselect (to allow multiple selections).
Code:
'''Adapted from tek-tips.com/faqs.cfm?fid=2484'''
' Code to call dialog box
Dim cmdlgOpenFile As New clsCommonDialog
Const clngFilterIndexAll = 5
' Allow multiple files to be selected, using long file names
cmdlgOpenFile.Flags = cdlOFNExplorer Or cdlOFNAllowMultiselect
' Set filters
cmdlgOpenFile.Filter = "Excel Files (*.xls)|*.xls|All Files (*.*)|*.*"
cmdlgOpenFile.FilterIndex = clngFilterIndexAll
' Opens the dialog box
cmdlgOpenFile.ShowOpen
My understanding is that if long file names are used, then the code will return a single string containing the path and file names delimited by a vbNull.
To test the result of selecting multiple files I used this code (which if I understand it correctly, should split the returned string into the path and individual file names.
Code:
'''Adapted from answers.yahoo.com/question/index?qid=20071203135914AA0cY8r'''
Dim strFiles() As String 'array to hold the split files
Dim intCnt As Integer ' count of array elements
Dim Cnt As Integer ' used to index through the array
Dim sep As String 'delimiter character
sep = Chr(0) 'define a null seperator
With cmdlgOpenFile
Debug.Print (.fileName)
strFiles = Split(.fileName, sep)
intCnt = UBound(strFiles)
For Cnt = 0 To intCnt
Debug.Print (strFiles(Cnt))
Next Cnt
End With
(Note: rather than defining my own sep variable, I have also tried using vbNull and vbNullChar as the delimiter, with identical results)
When I run the code, if I select one file, I get the intended result in the immediate window:
C:\Documents and Settings\xxxxxxxxxx\Desktop\filename1.xls
C:\Documents and Settings\xxxxxxxxxx\Desktop\filename2.xls
However, if I select multiple files, I get the following result in the immediate window:
C:\Documents and Settings\xxxxxxxxxx\Desktop
C:\Documents and Settings\xxxxxxxxxx\Desktop
It does not matter if I select two, three, or ten files; the Debug.Print only runs twice, and it only returns the path with no file names.
I also tried saving the whole string returned into a single string variable, and the using MsgBox "variableName" to test the code; however, if I select multiple files, this only returns the path as well. Additionally, I modified the code above as follows:
Code:
With cmdlgOpenFile
Debug.Print (.fileName)
strFiles = Split(.fileName, sep)
intCnt = UBound(strFiles)
Debug.Print (strFiles(0))
Debug.Print (strFiles(1))
Debug.Print (strFiles(2))
End With
The result in the immediate window should be:
C:\Documents and Settings\xxxxxxxxxx\Desktop
filename1.xls
filename2.xls
However, what I get is an out of bounds error when I reach strFiles(1). It appears that if one file is selected the code works properly, but if more than one file is selected the code only returns the path and not the file names.
How can I get the code to return both the path and the file name for each file selected, and assign the individual file names and paths to a collection, array, or individual variables that I can then reference in the rest of my code?
Any help you can provide would be greatly appreciated...