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

Article contributed by Jonathan West

There are three 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.

Go to www.mvps.org/ccrp/ and download their BrowseDialog Server component (it's free) and use that. It gives you a fully configurable browse for folders dialog which you treat in much the same way as you treat a class.

It a bit more complex to use, but looks much more professional.

3.

Go to http://www.mvps.org/btmtz/browsdlg/ and pick up the BrowseDlg code by Brad Martinez.

This gives you the same capability as the BrowseDialog Server, except that you can't have a New folder button on the dialog. The advantage is that you don't have to distribute an additional DLL with your template, everything is included within your code.

4.

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