- Open Dialog (msoFileDialogOpen) - allows users to select one or more files. If you choose to do so, you can then open the selected file or files in the host application by using the Execute method.
- SaveAs Dialog (msoFileDialogSaveAs) - allows users to select a single file. If you choose to do so, you can then save the current file by using the Execute method.
- File Picker Dialog (msoFileDialogFilePicker) - allows users to select one or more files. The file paths that the user selects are captured in the FileDialogSelectedItems collection.
- Folder Picker Dialog (msoFileDialogFolderPicker) - allows users to select a folder. The path for the selected folder is captured in the FileDialogSelectedItems collection.
Once you have a reference to a FileDialog object, you can display the dialog box by using the Show method. All FileDialog objects are modal so once a dialog box is displayed, no code continues to execute until the dialog box is dismissed. Your code can also check the return value of the Show method to determine if the user accepts or cancels the file (or folder) selection and then to handle that condition as needed.
The following sample VBA code creates and shows a File Picker dialog box. When you run the code to display the dialog box, select one or more files and click OK to dismiss the dialog box. Each of your file selections is displayed in a message box. Note that you can use the CTRL+SHIFT keyboard combination in the dialog box to select multiple files.
'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 a String,
'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.
'Use the Show method to display the File Picker dialog box
'The user pressed the action button.
If .Show = -1 Then
'Step through the FileDialogSelectedItems collection.
For Each vrtSelectedItem In .SelectedItems
'vrtSelectedItem contains the path of each selected item.
'Here use any file I/O functions you want on the path.
'This example simply displays the path in a message box.
MsgBox "The path is: " & vrtSelectedItem
'The user pressed Cancel.
'Set the object variable to Nothing.
Set fd = Nothing
Because a file dialog box can only be accessed by calling the FileDialog property of an Office Application object, use of an Office XP file dialog box requires a running instance of an Office XP application. Therefore, you cannot use the Office XP file dialog boxes from a non-Office application without Automation to an Office application.
ID do Artigo: 288543 - Última Revisão: 23 de mar de 2009 - Revisão: 1