Multiselect File dialogbox

Posted on 2004-08-11
Last Modified: 2007-12-19
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.
Question by:knollbert
  • 2
LVL 27

Accepted Solution

jjafferr earned 500 total points
ID: 11778237
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

Author Comment

ID: 11783098
Thanks I had something similar but would only do old 95 style for multiselect
Searched here to but could find anything

LVL 27

Expert Comment

ID: 11784278
You are welcome,
thanks for the points and the grade.


Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now