Getting a Folder Name

Sometimes, you don't need to get a filename, you need to get a folder name. If that's the case, the FileDialog object is just what the doctor ordered.

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

Sub

GetAFolder()

With Application.FileDialog(msoFileDialogFolderPicker)

.InitialFileName = Application.DefaultFilePath &

.Title = "

Please select a location for the backup"

.Show

If .SelectedItems.Count = 0 Then

MsgBox

"Canceled"

Else

MsgBox

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

0 0

Post a comment