[RESOLVED] CommonDialog: error when selecting multiple files
I'm selecting multiple files by means of a common dialog and get error 20476 which means the .FileName property buffer is too small to hold all the file names. However I had selected only 6 files with 28 characters each. Anyone knows the character limitation? Is there any other reason why this should fail?
Re: CommonDialog: error when selecting multiple files
A peek at your code would have helped :)
However this is something which I often use....
http://support.microsoft.com/kb/198974
See if this helps...
Re: CommonDialog: error when selecting multiple files
This works just fine for me
Code:
Private Sub Command1_Click()
Dim sArrFileList() As String
Dim i As Long
With CommonDialog1
.CancelError = False
.InitDir = "C:\"
.Filter = "All Files (*.*)|*.*|Text Files (*.txt)|*.txt|" & _
"Batch Files (*.bat)|*.bat|" & _
"Word Documents (*.doc)|*.doc"
.Flags = cdlOFNAllowMultiselect + cdlOFNExplorer
.ShowOpen
End With
sArrFileList = Split(CommonDialog1.FileName, Chr(0))
For i = 0 To UBound(sArrFileList())
List1.AddItem sArrFileList(i)
Next
'also addes the drive
'letter which we
'do not want
List1.RemoveItem 0
End Sub
Re: CommonDialog: error when selecting multiple files
Instead of adding the first item which is the C:\ then removing it afterwards, you could just Loop starting at 1 instead of 0.
Re: CommonDialog: error when selecting multiple files
Quote:
Originally Posted by koolsid
I don't think the code is (completely) wrong as it works in some instances like fewer files at a time.
VB Code:
'...
FilterText = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
If Not GetFileNames(FilterText) Then Exit Sub
'...
'
Private Function GetFileNames(ftxt) As Boolean
Dim i As Integer
Dim chunk() As String
Dim dirstr As String
On Error GoTo GetFileNames_Error
GetFileNames = False
With CommonDialog1
.CancelError = True
'File name must be reset to allow browsing to a different initial
'directory because the common dialog remembers the last one used
.FileName = ""
'Allow multiple selection
.flags = cdlOFNAllowMultiselect Or cdlOFNExplorer
'(DataDir is a public variable initialized elsewhere)
.InitDir = DataDir
.Filter = ftxt
.Action = 1 'Open
End With
chunk = Split(.FileName, Chr(0))
If UBound(chunk) = 0 Then
'Only one file was selected
'selFiles() is a string array declared at form level
ReDim selFiles(0)
selFiles(0) = chunk(0)
Else
'Multiple files were selected
dirstr = chunk(0) & "\"
ReDim selFiles(UBound(chunk) - 1)
For i = 1 To UBound(chunk)
selFiles(i - 1) = dirstr & chunk(i)
Next
End If
GetCalFiles = True
Exit Function
GetFileNames_Error:
'Selection cancelled out
Files = ""
On Error GoTo 0
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure GetFileNames"
End Function
Re: CommonDialog: error when selecting multiple files
commondialog has a maxfilesize property that allows you to increase the size of the returned string
the default is 256 characters, can be up to 32k
from msdn
Quote:
Remarks
The MaxFileSize property allocates memory to store the actual names of the selected file or files. When using the cdlOFNAllowMultiselect flag, you may want to increase the size of the MaxFileSize property to allow enough memory for the selected file names.
Re: CommonDialog: error when selecting multiple files
Quote:
Originally Posted by westconn1
commondialog has a maxfilesize property that allows you to increase the size of the returned string
the default is 256 characters, can be up to 32k...
That was it :thumb: