How to allow the user to browse to and select a folder

Article contributed by Jonathan West / updated by Lene Fredborg

There are two options available.

1.

Use the Copy File dialog:

With Dialogs(wdDialogCopyFile)
    If .Display <> 0 Then
        MsgBox "You chose " & .Directory
    Else
        MsgBox "Dialog cancelled"
    End If
End With

The advantage is that it is quick & simple, but the disadvantage is that you can't change the caption of the dialog, which says Copy.

2.

If you are using Word 2002 (Office XP) or later, you can use the built-in FileDialog object.

With Application.FileDialog(msoFileDialogFolderPicker)

    .AllowMultiSelect = False

    If .Show <> 0 Then

        MsgBox "You chose " & .SelectedItems(1)

    Else

        MsgBox "Dialog cancelled"

    End If

End With

The above is a simple code sample, you can do more in terms of setting the dialog caption and the starting folder. Look up the details in the VBA Help.


Click to view Terms of Use page

Click to view Disclaimer page