VBA/Excel/Access/Word/File Path/GetOpenFilename

Материал из VB Эксперт
Версия от 12:47, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Application.GetOpenFilename

 
Sub ManyFiles()
    Dim x As Variant
    x = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls*), *.xls*",Title:="Choose Files", MultiSelect:=True)
    
    On Error Resume Next
    If x = "False" Then Exit Sub
    On Error GoTo 0
    
    For i = 1 To UBound(x)
        MsgBox "You selected " & x(i)
    Next i
End Sub



Get the selected file name in a file open dialog

 
Sub GetImportFileName()
    Filt = "Text Files (*.txt),*.txt," & _
           "Lotus Files (*.prn),*.prn," & _
           "Comma Separated Files (*.csv),*.csv," & _
           "ASCII Files (*.asc),*.asc," & _
           "All Files (*.*),*.*"
    FilterIndex = 5
    Filename = Application.GetOpenFilename (FileFilter:=Filt, FilterIndex:=FilterIndex, Title:="Select a File to Import")
    If Filename = False Then
        MsgBox "No file was selected."
        Exit Sub
    End If
    Debug.Print Filename
End Sub



Load excel file

 
Sub LoadExcelFile()
  Dim result
  result = Application.GetOpenFilename("Excel files,*.xl?", 1)
  If result = False Then Exit Sub
  Workbooks.Open result
End Sub



Selecting multiple files

 
  Sub GetImportFileName2()
      Dim FileNames As Variant
      Dim Msg As String
      Dim I As Integer
      FileNames = Application.GetOpenFilename(MultiSelect:=True)
      If IsArray(FileNames) Then
          Msg = "You selected:" & vbNewLine
          For I = LBound(FileNames) To UBound(FileNames)
              Msg = Msg & FileNames(i) & vbNewLine
          Next i
          MsgBox Msg
      Else
          MsgBox "No files were selected."
      End If
  End Sub



Use filter in Open file dialog

 
Sub GetImportFileName2()
    Filt = "Text Files (*.txt),*.txt," & "Lotus Files (*.prn),*.prn," & "Comma Separated Files (*.csv),*.csv," & "ASCII Files (*.asc),*.asc," & "All Files (*.*),*.*"
    FilterIndex = 5
    
    Filename = Application.GetOpenFilename (FileFilter:=Filt, FilterIndex:=FilterIndex, Title:="Select a File to Import",MultiSelect:=True)
    If Not IsArray(Filename) Then
        MsgBox "No file was selected."
        Exit Sub
    End If
    For i = LBound(Filename) To UBound(Filename)
        Debug.Print Filename(i)
    Next i
End Sub