- Public Function PickFileOrFolder(Optional IsFile = True, Optional MultiFile = False, Optional DialogTitle = "Please select...") As String
- '===================================================================================
- ' Created by: Sanda Vladescu
- ' Updated on:
- ' Purpose:
- ' References: Microsoft ActiveX Data object...
- ' Usage example:
- ' arrFile=split(PickFileOrFolder(1,1),"|")
- '===================================================================================
- Dim FileOrFolder
- Dim BrowserFolder, MyPath As String, item, itemNb
- If IsFile Then
- FileOrFolder = msoFileDialogFilePicker
- Else
- FileOrFolder = msoFileDialogFolderPicker
- End If
- Set BrowserFolder = Application.FileDialog(FileOrFolder)
- With BrowserFolder
- .AllowMultiSelect = MultiFile
- .InitialFileName = Application.DefaultFilePath
- .Title = DialogTitle
- .Show
- If MultiFile Then
- itemNb = 0
- For Each item In Application.FileDialog(FileOrFolder).SelectedItems
- itemNb = itemNb + 1
- MyPath = MyPath & Application.FileDialog(FileOrFolder).SelectedItems(itemNb) & "|"
- Next item
- Else
- MyPath = Application.FileDialog(FileOrFolder).SelectedItems(1)
- End If
- End With
- PickFileOrFolder = MyPath
- End Function
Then, you can use this to get the file name or the path.