2009-07-07 122 views

回答

38

我對Renaud Bompuis的回答的評論搞砸了。

實際上,您可以使用後期綁定,並且不需要對11.0對象庫的引用。

下面的代碼將工作沒有任何參考資料:

Dim f As Object 
Set f = Application.FileDialog(3) 
f.AllowMultiSelect = True 
f.Show 

MsgBox "file choosen = " & f.SelectedItems.Count 

注意上面的作品以及在運行時也。

+2

+1始終希望這是可能的,關鍵是要使後期的綁定工作是通過數字選項,而不是msoOpenFileDialog等。這麼簡單但很好的答案:) – 2012-07-26 19:48:14

17

在Access 2007中,您只需使用Application.FileDialog

下面是訪問文檔的例子:

' Requires reference to Microsoft Office 12.0 Object Library. ' 
Private Sub cmdFileDialog_Click() 
    Dim fDialog As Office.FileDialog 
    Dim varFile As Variant 

    ' Clear listbox contents. ' 
    Me.FileList.RowSource = "" 

    ' Set up the File Dialog. ' 
    Set fDialog = Application.FileDialog(msoFileDialogFilePicker) 

    With fDialog 

     ' Allow user to make multiple selections in dialog box ' 
     .AllowMultiSelect = True 

     ' Set the title of the dialog box. ' 
     .Title = "Please select one or more files" 

     ' Clear out the current filters, and add our own.' 
     .Filters.Clear 
     .Filters.Add "Access Databases", "*.MDB" 
     .Filters.Add "Access Projects", "*.ADP" 
     .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 

     'Loop through each file selected and add it to our list box. ' 
     For Each varFile In .SelectedItems 
      Me.FileList.AddItem varFile 
     Next 

     Else 
     MsgBox "You clicked Cancel in the file dialog box." 
     End If 
    End With 
End Sub 

由於樣本說,只要確保你的的Microsoft Access 12.0對象庫引用(下VBE IDE>工具>參考菜單)。

+0

實際上,您可以使用後期綁定,並且不需要對11.0對象庫的引用。 下面的代碼將工作,而無需任何的引用: 昏暗˚F作爲對象 集合F = Application.FileDialog(3) f.AllowMultiSelect =真 f.Show MSGBOX 「文件choosen =」 &f.SelectedItems .Count 請注意,以上運作良好,我的運行時也。 Albert D.Kallal 埃德蒙頓,加拿大 [email protected] – 2009-07-09 02:25:50

+0

該示例從這裏複製:http://support.microsoft.com/en-us/kb/824272 – Mike 2015-04-08 02:40:18

2

加入到什麼艾伯特已經表示:

此代碼(各種樣品的混搭)提供有一個另存爲對話框

Function getFileName() As String 
    Dim fDialog As Object 
    Set fDialog = Application.FileDialog(msoFileDialogSaveAs) 
    Dim varFile As Variant 

    With fDialog 
     .AllowMultiSelect = False 
     .Title = "Select File Location to Export XLSx :" 
     .InitialFileName = "jeffatwood.xlsx" 

    If .Show = True Then 
     For Each varFile In .SelectedItems 
     getFileName = varFile 
     Next 
    End If 
End With 
End Function 
0

我同意約翰·M有最好的答案OP的能力題。思想沒有明確說明,明顯的目的是得到一個選定的文件名,而其他答案返回計數或列表。不過,我想補充一點,在這種情況下,msofiledialogfilepicker可能是更好的選擇。即:

Dim f As object 
Set f = Application.FileDialog(msoFileDialogFilePicker) 
dim varfile as variant 
f.show 
with f 
    .allowmultiselect = false 
    for each varfile in .selecteditems 
     msgbox varfile 
    next varfile 
end with 

注:varfile的值將保持不變,因爲多選是假的(只有一個項目是不斷選擇)。我在循環之外使用了它的價值,同樣成功。不過,John M所做的做法可能更好。此外,文件夾選取器可用於獲取選定的文件夾。我總是喜歡後期綁定,但我認爲該對象是原生默認訪問庫,所以它可能沒有必要在這裏

1

我有一個類似的解決方案,以上,它適用於打開,保存,文件選擇。我將它粘貼到它自己的模塊中,並用在我創建的所有Access數據庫中。如代碼所述,它需要Microsoft Office 14.0 Object Library。我想是另一種選擇:

Public Function Select_File(InitPath, ActionType, FileType) 
    ' Requires reference to Microsoft Office 14.0 Object Library. 

    Dim fDialog As Office.FileDialog 
    Dim varFile As Variant 


    If ActionType = "FilePicker" Then 
     Set fDialog = Application.FileDialog(msoFileDialogFilePicker) 
     ' Set up the File Dialog. 
    End If 
    If ActionType = "SaveAs" Then 
     Set fDialog = Application.FileDialog(msoFileDialogSaveAs) 
    End If 
    If ActionType = "Open" Then 
     Set fDialog = Application.FileDialog(msoFileDialogOpen) 
    End If 
    With fDialog 
     .AllowMultiSelect = False 
     ' Disallow user to make multiple selections in dialog box 
     .Title = "Please specify the file to save/open..." 
     ' Set the title of the dialog box. 
     If ActionType <> "SaveAs" Then 
      .Filters.Clear 
      ' Clear out the current filters, and add our own. 
      .Filters.Add FileType, "*." & FileType 
     End If 
     .InitialFileName = InitPath 
     ' Show the dialog box. If the .Show method returns True, the 
     ' user picked a file. If the .Show method returns 
     ' False, the user clicked Cancel. 
     If .Show = True Then 
     'Loop through each file selected and add it to our list box. 
      For Each varFile In .SelectedItems 
       'return the subroutine value as the file path & name selected 
       Select_File = varFile 
      Next 
     End If 
    End With 
End Function 
相關問題