MORE INFORMATION
To use a
FileDialog object, you must first call the
FileDialog property of an Office XP Application object and provide an argument for the
DialogType. There are four types of file dialogs:
- 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.
Each host Office application can only instantiate a single instance of a
FileDialog object. Many of the properties of the
FileDialog object persist even when you create multiple
FileDialog objects. Therefore, you should initialize all of the dialog properties appropriately for your purpose before you display the dialog box.
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.
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 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.
With fd
'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
Next vrtSelectedItem
'The user pressed Cancel.
Else
End If
End With
'Set the object variable to Nothing.
Set fd = Nothing
End Sub
After you call the
Show method to display a file dialog box, if the dialog box is type
msoFileDialogOpen or
msoFileDialogSaveAs, you can call the
Execute method to carry out the default Open or Save action. For example, if the file dialog box type is
msoFileDialogOpen, you can call the
Execute method to open the file selected by the user.
Additional Note
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.
REFERENCES
For additional information, click the article number below
to view the article in the Microsoft Knowledge Base:
279508 ACC2002: How to Display and Use the File Dialog Box in Microsoft Access 2002