Solved

VBA FileDialog - Set Multiple Selected Files to Array

Posted on 2013-11-26
3
2,271 Views
Last Modified: 2013-11-26
I'm trying to enable this code for file picker to select multiple file and return all of the filename.  I've used this code for sometime to return one file.  I'm trying to enable multiselect so I can return a list of selected files.

Can someone help me get there?

Public Function modImportExport_OpenFile(strFileExtension As String, Optional bolAllowMultiSelect As Boolean = False)
    ' the input is used do set the filtering on the dialog box
    ' Returns Filename/Path OR Canceled
    Dim objfd As Object
    Dim vrtSelectedItem As Variant
    Dim strExtType As String
    Set objfd = Application.FileDialog(3)
  
    Select Case strFileExtension
        Case "xls", "xlsx"
            strExtType = "*.xls,*.xlsx"
        Case "txt", "asc", "csv"
            strExtType = "*.txt,*.acs,*.csv,*.prn"
        Case Else
            strExtType = "*.*"
    End Select
    With objfd
        .Title = "Please Select Your File"
        .AllowMultiSelect = bolAllowMultiSelect
        .Filters.Clear
        .Filters.Add "Files", strExtType, 1

        If .show = -1 Then

           For Each vrtSelectedItem In .SelectedItems
             'Return File Name and Path
               modImportExport_OpenFile = vrtSelectedItem  '<< Something needs to happen here to put selected files (1 or more) into an array to return
           Next vrtSelectedItem
                      
        Else
        'The user pressed Cancel.
            modImportExport_OpenFile = "Canceled"
        End If
        
    End With
    'Set the object variable to Nothing.
    Set objfd = Nothing
End Function

Open in new window

0
Comment
Question by:keschuster
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 39678664
You could insert items into your array at that entry point, or you could concatenate the individual file names to a string, using a separator like ";".  Then after the loop is done, you could simply use the Split( ) function to populate your array:

Dim myArray() as string

'insert other code here

myArray = Split(varFileNames, ";")

I prefer the latter because I don't have to dimension the array beforehand and check to see whether I have exceeded that value.
0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 500 total points
ID: 39678672
try this revised codes

Public Function modImportExport_OpenFile(strFileExtension As String, Optional bolAllowMultiSelect As Boolean = False)
    ' the input is used do set the filtering on the dialog box
    ' Returns Filename/Path OR Canceled
    Dim objfd As Object
    Dim vrtSelectedItem As Variant
    Dim strExtType As String, strFiles As String
    Set objfd = Application.FileDialog(3)
  
    Select Case strFileExtension
        Case "xls", "xlsx"
            strExtType = "*.xls,*.xlsx"
        Case "txt", "asc", "csv"
            strExtType = "*.txt,*.acs,*.csv,*.prn"
        Case Else
            strExtType = "*.*"
    End Select
    With objfd
        .Title = "Please Select Your File"
        .AllowMultiSelect = bolAllowMultiSelect
        .Filters.Clear
        .Filters.Add "Files", strExtType, 1

        If .Show = -1 Then

           For Each vrtSelectedItem In .SelectedItems
             'Return File Name and Path
             strFiles = strFiles & "," & vrtSelectedItem
               modImportExport_OpenFile = vrtSelectedItem  '<< Something needs to happen here to put selected files (1 or more) into an array to return
           Next vrtSelectedItem
              modImportExport_OpenFile = Mid(strFiles, 2)
        Else
        'The user pressed Cancel.
            modImportExport_OpenFile = "Canceled"
        End If
        
    End With
    'Set the object variable to Nothing.
    Set objfd = Nothing
End Function
                                  

Open in new window

0
 

Author Comment

by:keschuster
ID: 39678681
so in my case where strFileList() = the array
Insert

           For Each vrtSelectedItem In .SelectedItems
               strFileList = Split(vrtSelectedItem, ";")  '<<< Insert this??
           Next vrtSelectedItem
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

732 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