Results 1 to 7 of 7

Thread: [RESOLVED] CommonDialog: error when selecting multiple files

  1. #1

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Resolved [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?
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    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...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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

  4. #4
    Hyperactive Member danecook21's Avatar
    Join Date
    Feb 2008
    Location
    NC, USA
    Posts
    501

    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.

  5. #5

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: CommonDialog: error when selecting multiple files

    Quote Originally Posted by koolsid
    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...
    I don't think the code is (completely) wrong as it works in some instances like fewer files at a time.

    VB Code:
    1. '...
    2.     FilterText = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
    3.     If Not GetFileNames(FilterText) Then Exit Sub
    4. '...
    5. '
    6. Private Function GetFileNames(ftxt) As Boolean
    7.     Dim i As Integer
    8.     Dim chunk() As String
    9.     Dim dirstr As String
    10.    
    11.     On Error GoTo GetFileNames_Error
    12.    
    13.     GetFileNames = False
    14.    
    15.     With CommonDialog1
    16.         .CancelError = True
    17.         'File name must be reset to allow browsing to a different initial
    18.         'directory because the common dialog remembers the last one used
    19.         .FileName = ""
    20.         'Allow multiple selection
    21.         .flags = cdlOFNAllowMultiselect Or cdlOFNExplorer
    22.          '(DataDir is a public variable initialized elsewhere)
    23.         .InitDir = DataDir
    24.         .Filter = ftxt
    25.         .Action = 1 'Open
    26.     End With
    27.     chunk = Split(.FileName, Chr(0))
    28.     If UBound(chunk) = 0 Then
    29.         'Only one file was selected
    30.         'selFiles() is a string array declared at form level
    31.         ReDim selFiles(0)
    32.         selFiles(0) = chunk(0)
    33.     Else
    34.         'Multiple files were selected
    35.         dirstr = chunk(0) & "\"
    36.         ReDim selFiles(UBound(chunk) - 1)
    37.         For i = 1 To UBound(chunk)
    38.             selFiles(i - 1) = dirstr & chunk(i)
    39.         Next
    40.     End If
    41.     GetCalFiles = True
    42.     Exit Function
    43.    
    44. GetFileNames_Error:
    45.     'Selection cancelled out
    46.     Files = ""
    47.     On Error GoTo 0
    48.     MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure GetFileNames"
    49.  
    50. End Function
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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
    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.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    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
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

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