1.0 VBA
Reference: Microsoft Scriptiong Runtime
1.1 FOLDER精简版
Private Function IChooseFolder() As String
Dim dlgOpen As FileDialog
Set dlgOpen = Application.FileDialog(msoFileDialogFolderPicker)
If dlgOpen.Show = -1 Then IChooseFolder = dlgOpen.SelectedItems(1)
Set dlgOpen = Nothing
End Function
1.2 FOLDER增强版
Private Function IFolderPicker() As String
Dim FolderDialogObject As FileDialog
Set FolderDialogObject = Application.FileDialog(msoFileDialogFolderPicker)
With FolderDialogObject
.Title = "Select your search folder"
.InitialFileName = "C:\"
.ButtonName = "Choose"
.Show 'FileDialog 对象的 Show 方法显示对话框,并且返回 -1(如果您按 OK)和 0(如果您按 Cancel)
End With
On Error Resume Next
IFolderPicker = FolderDialogObject.SelectedItems(1)
Set FolderDialogObject = Nothing
End Function
1.3 FILE单选
Private Function IFilePicker() As String
Dim FileDialogObject As FileDialog
Set FileDialogObject = Application.FileDialog(msoFileDialogFilePicker)
With FileDialogObject
.Title = "Select your search folder"
.InitialFileName = "C:\"
.ButtonName = "Choose"
.Filters.Clear
.Filters.Add "Excel Files", "*.xls;*.xlw"
.Filters.Add "All Files", "*.*"
.AllowMultiSelect = False '多选
.Show 'FileDialog 对象的 Show 方法显示对话框,并且返回 -1(如果您按 OK)和 0(如果您按 Cancel)
End With
On Error Resume Next
IFilePicker = FileDialogObject.SelectedItems(1)
Set FileDialogObject = Nothing
End Function
1.4 FILE多选
Private Sub IFileMultiPicker()
Dim FileDialogObject As FileDialog
Set FileDialogObject = Application.FileDialog(msoFileDialogFilePicker)
With FileDialogObject
.Title = "Select your search folder"
.InitialFileName = "C:\"
.ButtonName = "Choose"
.Filters.Clear
.Filters.Add "Excel Files", "*.xls;*.xlw"
.Filters.Add "All Files", "*.*"
.AllowMultiSelect = True '多选
.Show 'FileDialog 对象的 Show 方法显示对话框,并且返回 -1(如果您按 OK)和 0(如果您按 Cancel)
For i = 1 To .SelectedItems.Count
MsgBox "YourChoseFiles: " & .SelectedItems(i), vbOKOnly + vbInformation, "Tips"
Next
End With
Set FileDialogObject = Nothing
End Sub
1.5 String转Folder
Private Function IFolderCheck(iFldPath As String) As Folder
Dim tempFld As Folder
Dim fso As New FileSystemObject
Set tempFld = fso.GetFolder(iFldPath)
set IFolderCheck = tempFld
End Function
1.6 String转File
Private Function IFileCheck(iFilePath As String) As file
Dim tempFile As file
Dim fso As New FileSystemObject
Set tempFile = fso.GetFile(iFilePath)
Set IFileCheck = tempFile
End Function