Results 1 to 2 of 2

Thread: Dialog to select multiple files

  1. #1

    Thread Starter
    Lively Member mattalexx's Avatar
    Join Date
    Mar 2002
    Location
    Gloucester, MA
    Posts
    77

    Dialog to select multiple files

    In Access VBA, is there a way to bring up a Windows dialog from which you can select multiple files on your hard drive? I'd like it to return a string with the path and filenames, separated with commas or something.

    Is this, or something like it, possible?

    Thanks in advance,
    Matt
    Matt Alexander
    [email protected]

    Don't click here.

    Odigo: 5408962
    AIM: mattalexx
    ICQ: 138006220

  2. #2
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343
    Yes, it is possible.

    VB Code:
    1. Public Type mOpenFilename
    2.     lStructSize As Long
    3.     hwndOwner As Long
    4.     hInstance As Long
    5.     lpstrFilter As String
    6.     lpstrCustomFilter As String
    7.     nMaxCustFilter As Long
    8.     nFilterIndex As Long
    9.     lpstrFile As String
    10.     nMaxFile As Long
    11.     lpstrFileTitle As String
    12.     nMaxFileTitle As Long
    13.     lpstrInitialDir As String
    14.     lpstrTitle As String
    15.     Flags As Long
    16.     nFileOffset As Integer
    17.     nFileExtension As Integer
    18.     lpstrDefExt As String
    19.     lCustData As Long
    20.     lpfnHook As Long
    21.     lpTemplateName As String
    22. End Type
    23.  
    24. Public Type mChooseColor
    25.     lStructSize As Long
    26.     hwndOwner As Long
    27.     hInstance As Long
    28.     rgbResult As Long
    29.     lpCustColors As String
    30.     Flags As Long
    31.     lCustData As Long
    32.     lpfnHook As Long
    33.     lpTemplateName As String
    34. End Type
    35.  
    36. '---- common dialog stuff :)
    37. Public Declare Function apiGetOpenFilename Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pmOpenFilename As mOpenFilename) As Long
    38. Public Declare Function apiGetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pmOpenFilename As mOpenFilename) As Long
    39. Public Declare Function apiGetChooseColor Lib "comdlg32.dll" Alias "ChooseColorA" (pmChooseColor As mChooseColor) As Long
    40.  
    41. '---- Selects a/several file(s) using common dialogs
    42. Public Function FileSelection(ByVal strInitDir As String, ByVal lngFlag As Long, ByVal strFilter As String, ByVal lngFilterIndex As Long, ByVal blnOpen As Boolean, Optional strTitle, Optional strFileExtension, Optional lngOwnerHwnd) As String
    43. '---- Selects one or more filenames
    44. '---- Requires
    45. '----   inital directory to open in
    46. '----   flags for opening
    47.  
    48.  
    49. '---- usual flags are :
    50. '----   &H200000 - use long filenames
    51. '----   &H80000 - Explorer type window
    52. '----   &H200 - multi select
    53. '----   &H4 - hide read only option on box - v useful
    54. '----   &H8 - force same dir as when opened
    55. '----   &H1000 - file must exist - useful for opening
    56. '---- usual I use : &H280004
    57.  
    58. '---- Returns a string holding either :
    59. '----   nothing
    60. '----   path and filename
    61. '----   path chr$(0) filenames separated by chr$(0)
    62. '---- NOTE : current max chars = 257 ... I think expand if neccessary
    63.  
    64.     Dim strTemp As String
    65.     Dim lngReturn As Long, lngP As Long, lngO As Long
    66.     Dim cOpenFilename As mOpenFilename
    67.  
    68.     On Error Resume Next
    69.  
    70.     With cOpenFilename
    71.  
    72. '---- Default values according to another developer
    73.         .lStructSize = Len(cOpenFilename)
    74.         .hInstance = 0
    75.         .nFilterIndex = 1
    76.         .nFileOffset = 0
    77.         .lpstrFile = String(5000, 0)
    78.         .nMaxFile = Len(.lpstrFile) - 1
    79.         .lpstrFileTitle = .lpstrFile
    80.         .nMaxFileTitle = .nMaxFile
    81.  
    82.         If Not IsMissing(strFileExtension) Then .lpstrDefExt = strFileExtension
    83.  
    84.  
    85. '---- Bits like the common dialog control
    86.         If blnOpen Then
    87.             .lpstrTitle = "Open a file..."
    88.         Else
    89.             .lpstrTitle = "Save file as..."
    90.         End If
    91.         If Not IsMissing(strTitle) Then .lpstrTitle = strTitle
    92.  
    93. '---- messing
    94. '----   default filter
    95.  
    96. '---- each filter is separated by a character of 0 - Name - filter - name filter (etc..)
    97. '---- example :
    98. '        .lpstrFilter = "All Files" & Chr$(0) & "*.*" & Chr$(0) & "Text Files" & Chr$(0) & "*.txt;*.csv"
    99. '---- replace filter with the selection chosen by the programmer...
    100.         .lpstrFilter = "All Files (*.*)" & Chr$(0) & "*.*"
    101.  
    102.         lngO = 1
    103.         lngP = InStr(1, strFilter, "|")
    104.         If lngP > 0 Then
    105.             strTemp = ""
    106.             Do Until lngP = 0
    107.                 strTemp = strTemp & IIf(Len(strTemp) > 0, Chr$(0), "") & Mid$(strFilter, lngO, lngP - lngO)
    108.                 lngO = lngP + 1
    109.                 lngP = InStr(lngP + 1, strFilter, "|")
    110.             Loop
    111.             strTemp = strTemp & Chr$(0) & Right$(strFilter, Len(strFilter) - lngO + 1)
    112.         Else
    113.             strTemp = strFilter
    114.         End If
    115.         .lpstrFilter = strTemp
    116.  
    117.         .Flags = lngFlag
    118.  
    119.         .hwndOwner = 0
    120.         If Not IsMissing(lngOwnerHwnd) Then .hwndOwner = lngOwnerHwnd
    121.  
    122.         .lpstrInitialDir = strInitDir
    123.  
    124.     End With
    125.  
    126. '---- is the dialog box an open or save?
    127.     If blnOpen Then
    128.         lngReturn = apiGetOpenFilename(cOpenFilename)
    129.     Else
    130.         lngReturn = apiGetSaveFileName(cOpenFilename)
    131.     End If
    132.  
    133. '---- send back the selected file(s)
    134.     If lngReturn = 0 Then
    135.         FileSelection = ""
    136.     Else
    137.         FileSelection = RemoveNonPChars(cOpenFilename.lpstrFile)
    138.     End If
    139.  
    140. End Function
    141.  
    142. Public Function RemoveNonPChars(ByVal strText As String) As String
    143. '---- gets rid of the extra chr$(0)'s in the text
    144. '---- by looking for two chr$(0)'s together (only happens at the end...)
    145. '---- NOTE : only removes those at the end of the string - for multi means that you can get the filenames...
    146.     Dim lngP As Long
    147.  
    148.     On Error Resume Next
    149.  
    150.     RemoveNonPChars = " "
    151.     If Len(strText) = 0 Then Exit Function
    152.     lngP = InStr(1, strText, Chr$(0) & Chr$(0))
    153.     If lngP > 1 Then RemoveNonPChars = Left$(strText, lngP - 1)
    154.     If Not Err.Number = 0 Then
    155.         RemoveNonPChars = " "
    156.         Err.Clear
    157.     End If
    158. End Function

    If you use this (its a version which I rewrote using someone elses base code, that at the time I didn't understand, and the MSDN libraries.

    If you use multi select and pick one file, the filename is as normal (like [i]C:\Test.Txt[i/]). If you use multi select and pick several files, they are returned with Chr$(0) as separators (C:\(0)Test1.Txt(0)Test2.txt)


    Vince

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

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