Using the File Dialog object to select a directory

If users of your application all use Excel 2002 or later, you might prefer to use a much simpler technique that makes use of the FileDialog object.

The FileDialog object was introduced in Excel 2002.Therefore,this technique will not work with earlier versions of Excel.

The following procedure displays a dialog box, which allows the user to select a directory. The selected directory name (or Canceled) is then displayed by using the MsgBox function.

Sub GetAFolder2() ' For Excel 2002 and later

With Application.FileDialog(msoFileDialogFolderPicker) .InitialFileName = Application.DefaultFilePath & "\" .Title = "Please select a location for the backup" .Show

If .SelectedItems.Count = 0 Then MsgBox "Canceled"


MsgBox .SelectedItems(l) End If End With End Sub

The FileDialog object lets you specify the starting directory by specifying a value for the InitialFileName property. In this case, the code uses Excel's default file path as the starting directory.

Was this article helpful?

0 0

Post a comment