Getting a Folder Name. Microsoft Excel VBA Programming
Reklama
Reklama
Table 15-6
Argument
InitialFileName
FileFilter
Chapter 15: Simple Dialog Boxes
249
GetSaveAsFilename Method Arguments
What It Does
Specifies a default filename that appears in the File
Name box.
Determines the types of files Excel displays in the dialog box (for example, *.TXT). You can specify several different filters from which the user can choose.
Determines which of the file filters Excel displays by default.
Defines a caption for the dialog box’s title bar.
FilterIndex
Title
Getting a Folder Name
Sometimes, you don’t need to get a filename; you just 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 .SelectedItems(1)
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.
Stiahnuť
Reklama