2012-04-24 142 views
15

有沒有辦法從vba表單打開一個Windows資源管理器窗口,導航到一個特定的文件並選擇它,以便文件名放置在文本框中?打開Windows資源管理器並選擇一個文件

+3

在VBA Excel中按魔術鍵'F1'並搜索'Application.GetOpenFilename';) – 2012-04-24 19:51:33

回答

46

看看這個片斷:

Private Sub openDialog() 
    Dim fd As Office.FileDialog 

    Set fd = Application.FileDialog(msoFileDialogFilePicker) 

    With fd 

     .AllowMultiSelect = False 

     ' Set the title of the dialog box. 
     .Title = "Please select the file." 

     ' Clear out the current filters, and add our own. 
     .Filters.Clear 
     .Filters.Add "Excel 2003", "*.xls" 
     .Filters.Add "All Files", "*.*" 

     ' Show the dialog box. If the .Show method returns True, the 
     ' user picked at least one file. If the .Show method returns 
     ' False, the user clicked Cancel. 
     If .Show = True Then 
     txtFileName = .SelectedItems(1) 'replace txtFileName with your textbox 

     End If 
    End With 
End Sub 

我覺得這是你所要求的東西。

+13

你是一個美麗的人。 – Quintis555 2012-04-24 20:08:16

相關問題