共用方式為


FileDialog.InitialFileName 屬性 (Office)

設定或傳回代表檔案對話方塊中最初顯示的路徑或檔案名稱的字串。 可讀寫。

語法

expression.InitialFileName

expression 代表 FileDialog 物件的變數。

註解

*指定檔案名時使用 和 ? 萬用字元,但在指定路徑時則不使用 。 * 符號代表任何數目的連續字元,而 ? 代表一個字元。 例如,.InitialFileName = "c:\c*s.txt" 會傳回 "charts.txt" 和 "checkregister.txt"。

如果指定路徑但未指定檔案名稱,對話方塊中會顯示檔案篩選器容許的所有檔案。

如果指定位於初始資料夾中的某個檔案,對話方塊中只會顯示那個檔案。

如果您指定的初始資料夾中不存在的檔案名稱,對話方塊就會包含任何檔案。 您在 InitialFileName屬性中指定的檔案類型會覆寫檔案篩選器設定。

如果指定無效路徑,則會使用上次使用的路徑。 使用無效路徑時,會顯示訊息警告使用者。

如果將這個屬性設為 256 個字元以上的字串,會產生執行階段錯誤。

範例

下列範例會使用 FileDialog 物件顯示 [檔案選擇器] 對話方塊,並在訊息方塊中顯示每個選取的檔案。

Sub Main() 
 
 'Declare a variable as a FileDialog object 
 Dim fd As FileDialog 
 
 'Create a FileDialog object as a File Picker dialog box. 
 Set fd = Application.FileDialog(msoFileDialogFilePicker) 
 
 'Declare a variable to contain the path 
 'of each selected item. Even though the path is aString, 
 'the variable must be a Variant because For Each...Next 
 'routines only work with Variants and Objects. 
 Dim vrtSelectedItem As Variant 
 
 'Use a With...End With block to reference the FileDialog object. 
 With fd 
 
 'Set the initial path to the C:\ drive. 
 .InitialFileName = "C:\" 
 
 'Use the Show method to display the File Picker dialog box and return the user's action. 
 'If the user presses the button... 
 If .Show = -1 Then 
 
 'Step through each string in the FileDialogSelectedItems collection. 
 For Each vrtSelectedItem In .SelectedItems 
 
 'vrtSelectedItem is aString that contains the path of each selected item. 
 'Use any file I/O functions that you want to work with this path. 
 'This example displays the path in a message box. 
 MsgBox "Selected item's path: " & vrtSelectedItem 
 
 Next vrtSelectedItem 
 'If the user presses Cancel... 
 Else 
 End If 
 End With 
 
 'Set the object variable to Nothing. 
 Set fd = Nothing 
 
End Sub 

另請參閱

支援和意見反應

有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應