Application.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 'Allow the selection of multiple files. .AllowMultiSelect = True 'Use the Show method to display the file picker dialog 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 'If the user presses Cancel... Else End If End With 'Set the object variable to Nothing. Set fd = Nothing End Sub
FileDialog.DialogType 属性 (Office)
获取一个MsoFileDialogType常量,该常量代表 FileDialog 对象设置为显示的文件对话框的类型。 此为只读属性。
示例
下面的示例将 FileDialog 对象视为未知类型,并且如果是 “另存为” 对话框或 “打开” 对话框,则运行 Execute 方法。(这句话也很重要)
Sub DisplayAndExecuteFileDialog(ByRef fd As FileDialog) 'Use a With...End With block to reference the FileDialog object. With fd 'If the user presses the action button... If .Show = -1 Then 'Use the DialogType property to determine whether to 'use the Execute method. Select Case .DialogType Case msoFileDialogOpen, msoFileDialogSaveAs: .Execute 'Do nothing otherwise. Case Else End Select 'If the user presses Cancel... Else End If End With End Sub
Application.FileDialog 属性 (Excel)
MsoFileDialogType 可为下述常量之一:
msoFileDialogFilePicker。 允许用户选择文件。
msoFileDialogFolderPicker。 允许用户选择文件夹。
msoFileDialogOpen。 允许用户打开文件。
msoFileDialogSaveAs。 允许用户保存文件。
FileDialog.Show 方法 (Office)
显示文件对话框并返回一个 Long 类型的值,指示用户按下的是“操作”按钮 (-1) 还是“取消”按钮 (0)。
如果调用 Show 方法,则在用户关闭文件对话框之前将不再执行其他代码。
对于“打开”和“另存为”对话框,请在调用 Show 方法之后立即使用 Execute 方法来执行用户操作。
FileDialog.Execute 方法 (Office)
在调用 Show 方法后立即执行用户的操作。
FileDialog.AllowMultiSelect 属性
此属性对 “文件夹选取器” 对话框或 “另存为” 对话框无效,因为用户永远无法从这些类型的文件对话框中选择多个文件。
FileDialog.Filters 属性 (Office)
以下示例使用 FileDialog 对象显示“文件选取器”对话框,并在消息框中显示每个选定的文件。 该示例还将添加一个名为 Images 的新文件筛选器。
Sub Main() 'Declare a variable as a FileDialog object. Dim fd As FileDialog 'Create a FileDialog object as a File Picker dialog. 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 'Add a filter that includes GIF and JPEG images and make it the first item in the list. .Filters.Add "Images", "*.gif; *.jpg; *.jpeg", 1 '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
FileDialog.SelectedItems 属性 (Office)
获取 FileDialogSelectedItems 集合。 此集合包含用户在使用 FileDialog 对象的 Show 方法显示的文件对话框中所选的文件的路径列表。 此为只读属性。
FileDialog.InitialFileName 属性 (Office)
设置或返回一个 String 类型的值,代表文件对话框中初始显示的路径或文件名。 读/写。

浙公网安备 33010602011771号