• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 831
  • Last Modified:

Multiselect File dialogbox

I need a dialog box to open files.

    do multi-select
It Cannot
    mangle file names (ie. myDocu~1).

It doesn't matter how it works ActiveX controls are less desirable
 but if necessary I can autoinstall them on access startup.
  • 2
1 Solution
Here you go, from:

 Dim sFolder As String
  Dim fso As Scripting.FileSystemObject
  Dim aFiles As Variant
  sFolder = CurrentProject.Path

  Set fso = New Scripting.FileSystemObject

  aFiles = apiBrowseFiles("Add Attachment Files", sFolder)

win32_BrowseFiles (Module)
Option Compare Database
Option Explicit

Private Type tOpenFilename
  lStructSize As Long                  ' length, in bytes, of the structure
  hWndOwner As Long                    ' window owner (or Null ie. 0 ?)
  hInstance As Long                    ' for customised dialog templates
  lpstrFilter As String                ' Pointer to a buffer containing pairs of null-terminated filter strings. The last string in the buffer must be terminated by two NULL characters
  lpstrCustomFilter As String          ' for preserving user-defined filter patterns at run-time
  nMaxCustFilter As Long               ' size of lpstrCustomFilter
  nFilterIndex As Long                 ' index of the currently selected filter in the File Types control
  lpstrFile As String                  ' initial File Name. returns full path of selected file
  nMaxFile As Long                     ' size of lpstrFile (should be at least 256 characters)
  lpstrFileTitle As String             ' returns filename and extension (without path information) of the selected file
  nMaxFileTitle As Long                ' size of lpstrFileTitle
  lpstrInitialDir As String            ' initial file directory
  lpstrTitle As String                 ' size of lpstrInitialDir
  Flags As Long
  nFileOffset As Integer               ' zero-based offset to file name in lpstrFile
  nFileExtension As Integer            ' zero-based offset to extension in lpstrFile
  lpstrDefExt As String                ' GetOpenFileName and GetSaveFileName append this extension to the filename if the user fails to type an extension
  lCustData As Long                    ' for hook procedure
  lpfnHook As Long                     ' for hook procedure
  lpTemplateName As String             ' for customised dialog templates
End Type

Private Const OFN_ALLOWMULTISELECT = &H200    ' List box allows multiple selections. If you also set the OFN_EXPLORER flag, the dialog box uses the Explorer-style user interface; otherwise, it uses the old-style user interface
                                              ' If the user selects more than one file, the lpstrFile buffer returns the path to the current directory followed by the filenames of the selected files. The nFileOffset member is the offset, in bytes or characters, to the first filename, and the nFileExtension member is not used.
                                              ' For Explorer-style dialog boxes, the directory and filename strings are NULL separated, with an extra NULL character after the last filename. This format enables the Explorer-style dialogs to return long filenames that include spaces.
                                              ' For old-style dialog boxes, the directory and filename strings are separated by spaces and the function uses short filenames for filenames with spaces
Private Const OFN_ENABLEHOOK = &H20           ' for WM_NOTIFY messages sent to the hook procedure
Private Const OFN_ENABLETEMPLATE = &H40       ' for customised dialog templates
Private Const OFN_ENABLETEMPLATEHANDLE = &H80 ' for customised dialog templates
Private Const OFN_EXPLORER = &H80000          ' This flag is necessary only if you provide a hook procedure or custom template, or set the OFN_ALLOWMULTISELECT flag
Private Const OFN_EXTENSIONDIFFERENT = &H400  ' for GetSaveFileName - user typed a filename extension that differs from the extension specified by lpstrDefExt
Private Const OFN_FILEMUSTEXIST = &H1000      ' If this flag is specified, the OFN_PATHMUSTEXIST flag is also used
Private Const OFN_HIDEREADONLY = &H4          ' Hides the Read Only check box
Private Const OFN_LONGNAMES = &H200000        ' default?
Private Const OFN_NOCHANGEDIR = &H8           ' Restores the current directory to its original value if the user changed the directory while searching for files
Private Const OFN_NODEREFERENCELINKS = &H100000  ' Return the path and filename of the selected shortcut (.LNK) file
Private Const OFN_NOLONGNAMES = &H40000       ' Explorer-style dialog boxes ignore this flag and always display long filenames
Private Const OFN_NONETWORKBUTTON = &H20000   ' Hides and disables the Network button
Private Const OFN_NOREADONLYRETURN = &H8000   ' returned file does not have the Read Only check box checked and is not in a write-protected directory.
Private Const OFN_NOTESTFILECREATE = &H10000  ' This flag should be specified if the application saves the file on a create-nonmodify network share
Private Const OFN_NOVALIDATE = &H100          ' allow invalid characters in the returned filename - for hook procedure?
Private Const OFN_OVERWRITEPROMPT = &H2       ' Save As dialog box to generate a message box if the selected file already exists
Private Const OFN_PATHMUSTEXIST = &H800       ' User can type only valid paths and filenames
Private Const OFN_READONLY = &H1              ' State of the Read Only check box
Private Const OFN_SHAREAWARE = &H4000         ' OpenFile network sharing violation error is ignored else notifies hook procedure
Private Const OFN_SHARENOWARN = 1
Private Const OFN_SHAREWARN = 0
Private Const OFN_SHOWHELP = &H10

Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As tOpenFilename) As Long

' GetOpenFileNamePreview ?

Public Function apiBrowseFiles( _
  Optional ByVal Title As String = "Open", _
  Optional ByVal InitialDir As String = "", _
  Optional ByVal InitialFile As String = "", _
  Optional ByVal Filter As String = "", _
  Optional ByVal FilterIndex As Integer = 1 _
  ) As Variant
  ' browse for muliple files and return array of files
  ' return values
  ' - cancel           - returns an Empty
  ' - one file         - Array("file name")
  ' - multiple files   - Array("file-name","file-name",...)
  Dim OpenFile As tOpenFilename
  Dim lFlags As Long
  Dim lReturn As Long
  Dim nNullPos As Integer
  Dim sFolder As String
  Dim sFiles As String
  Dim sFile As String
  '  Dim fso As Scripting.FileSystemObject
  '  Dim f As Scripting.File
  Dim aFiles() As Variant
  Dim n As Integer
  apiBrowseFiles = Empty
  If Filter = "" Then
    Filter = "All Files (*.*)" & vbNullChar & "*.*" & vbNullChar & vbNullChar
  End If
  If InitialDir = "" Then
    InitialDir = CurrentProject.Path    '& "\"
    '    Do While Right(InitialDir, 1) <> "\"
    '      InitialDir = Left(InitialDir, Len(InitialDir) - 1)
    '    Loop
  End If
  '  Flags = Flags Or OFN_EXPLORER
  'End If
  With OpenFile
    .lStructSize = Len(OpenFile)
    .hWndOwner = Application.hWndAccessApp
    .lpstrFilter = Filter
    .nFilterIndex = FilterIndex
    .lpstrFile = InitialFile & String(8000, 0)
    .nMaxFile = Len(.lpstrFile) - 1
    .lpstrFileTitle = String(256, 0)
    .nMaxFileTitle = Len(.lpstrFileTitle) - 1
    .lpstrInitialDir = InitialDir
    .lpstrTitle = Title
    .Flags = lFlags
  End With
  lReturn = GetOpenFileName(OpenFile)
  If lReturn <> 0 Then
    ' There are multiple files if the first NullChar is before the first file name
    sFiles = OpenFile.lpstrFile
    nNullPos = InStr(sFiles, vbNullChar)
    If nNullPos > OpenFile.nFileOffset Then
      ' one file
      sFiles = Left(sFiles, nNullPos - 1)
      ReDim aFiles(1 To 1)
      aFiles(1) = sFiles
      apiBrowseFiles = aFiles
      ' multiple files
      sFolder = Left(sFiles, nNullPos - 1)
      sFiles = Mid(sFiles, nNullPos + 1)
      nNullPos = InStr(sFiles, vbNullChar)
      ' Set fso = New Scripting.FileSystemObject
      n = 0
      Do While nNullPos > 1
        sFile = sFolder & "\" & Left(sFiles, nNullPos - 1)
        sFiles = Mid(sFiles, nNullPos + 1)
          ' Set f = fso.GetFile(sFile)
          ' If (f.Attributes And (Hidden Or System Or Alias)) = 0 _
          ' And f.Type <> "Shortcut" Then
          '  n = n + 1
          '  ReDim Preserve aFiles(1 To n)
          '  aFiles(n) = f.Path
          ' End If
        n = n + 1
        ReDim Preserve aFiles(1 To n)
        aFiles(n) = sFile
        nNullPos = InStr(sFiles, vbNullChar)
      If n > 0 Then
        ' apiBrowseFiles = aFiles
        ' GetOpenFileName returns file names in reverse order
        ReDim aReverse(1 To n) As Variant
        Dim i As Integer
        For i = 1 To n
          aReverse(n - i + 1) = aFiles(i)
        Next i
        apiBrowseFiles = aReverse
      End If
    End If
  End If
' problem with GetOpenFileName
' unable to delete folder of selected files until access app is quit
' - Sharing violation, the file may be in use.
End Function
knollbertAuthor Commented:
Thanks I had something similar but would only do old 95 style for multiselect
Searched here to but could find anything

You are welcome,
thanks for the points and the grade.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now