Sub example_01() 'msoFileDialogFilePicker
Dim f As String
With Application.FileDialog '(msoFileDialogFilePicker)
.Title = "Please select a file": .InitialFileName = ThisWorkbook.Path
'.Filters.Add "Excel", "*.xls;*.xlsx;*.xlsm", 1: .AllowMultiSelect = False
.Filters.Add "Text files", "*.csv;*.txt", 1: .AllowMultiSelect = False
If .Show = False Then Exit Sub: If .SelectedItems.Count = 0 Then Exit Sub
f = .SelectedItems(1)
End With
'[a1] = f
End Sub
Sub example_02() 'GetOpenFilename
Dim FilesToOpen
FilesToOpen = Application.GetOpenFilename _
(FileFilter:="Text Files (*.txt;*.csv), *.txt;*.csv", _
MultiSelect:=True, Title:="Text Files to Open")
If TypeName(FilesToOpen) = "Boolean" Then _
MsgBox "No Files were selected", 64: Exit Sub
MsgBox LBound(FilesToOpen) & " " & UBound(FilesToOpen)
End Sub
Sub example_03() 'msoFileDialogFolderPicker
Dim Fold As String, f As String
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Select the folder in which the files to be processed"
.ButtonName = "Select": .AllowMultiSelect = False
If .Show Then Fold = .SelectedItems(1) Else Exit Sub
End With
If Right(Fold, 1) <> "\" Then Fold = Fold & "\"
f = Dir(Fold & "*.xls*", vbNormal)
Do While f <> ""
MsgBox f
f = Dir()
Loop
End Sub
Sub example_04() 'GetSaveAsFilename
Dim NewName
NewName = Application.GetSaveAsFilename(FileFilter:="Excel file,*.xls*")
If NewName = "False" Then Exit Sub
MsgBox NewName
End Sub
Private Function FlSearchPDF(f As String) As String
With Application.FileDialog(msoFileDialogFilePicker)
.Title = "Выберите файл '" & f & "'": .InitialFileName = ThisWorkbook.Path
If .Filters.Count > 0 Then .Filters.Delete
.Filters.Add "PDF", "*.pdf", 1: .AllowMultiSelect = False
If .Show = False Then Exit Function: If .SelectedItems.Count = 0 Then Exit Function
FlSearchPDF = .SelectedItems(1)
End With
End Function
Sub example_05()
Dim f As String
f = FlSearchPDF("Справка")
CreateObject("WScript.Shell").Run """" & f & """"
End Sub
|