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 uses the FileDialog object.

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

JLti

Figure 12-10: Using the FileDialog object to select a directory.

Figure 12-10: Using the FileDialog object to select a directory.

Sub GetAFolder2() ' For Excel 2002 and later

With Application.FileDialog(msoFileDialogFolderPicker)

.InitialFileName = Application.DefaultFilePath & "\" .Title = "Please select a location for the backup" .Show

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

4 PREV

NEXT

0 0

Post a comment