Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

INFO: The Microsoft Office XP FileDialog Object

View products that this article applies to.


Microsoft Office XP provides a FileDialog object that gives you functionality that is similar to the standard Open and Save dialog boxes that are found in all Office XP applications. By using FileDialog objects in your Visual Basic for Application (VBA) solutions, you can easily provide a user interface for file selection or file naming without the need for additional ActiveX Controls or third-party solutions.

↑ Back to the top

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.
        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.

↑ Back to the top


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

↑ Back to the top

Keywords: KB288543, kbinfo

↑ Back to the top

Article Info
Article ID : 288543
Revision : 6
Created on : 1/31/2007
Published on : 1/31/2007
Exists online : False
Views : 403