Solved

VBA FileDialog - Set Multiple Selected Files to Array

Posted on 2013-11-26
3
2,362 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 48

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
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…

687 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