Chapter 4 - Using File Dialog Boxes
CHAPTER 4 - USING FILE DIALOG BOXES
4.1
Introduction
You can use the built-in dialog boxes in VBA to ask users to choose files and folders. An
example of a custom dialog box for opening files is shown below:
You can set a custom title
for the dialog box.
You can specify the initial
folder to look in.
You can choose which view
to display the files in.
You can customise the file
filters and choose the one
that should be applied by
default.
You can change the default
text on the action button.
Wise
Wise Owls
Owls
Hint
Hint
Copyright 2015
The exact style of the dialog box that you see will depend on which
operating system you are using. The example above is from Windows
Vista.
Page 35
Chapter 4 - Using File Dialog Boxes
4.2
Showing a Simple FileDialog
The easiest and most flexible way to open and save files in VBA is to use the FileDialog
property there are four types of dialog box you can use:
This is by far the easiest
way to open and save
files, and to choose files
and folders.
The four types of dialog box are:
Argument
What it does
msoFileDialogFilePicker
Lets users select one or more files
msoFileDialogFolderPicker
Lets users select a folder path
msoFileDialogOpen
Lets users select one or more files to open
msoFileDialogSaveAs
Lets users select a single file to save
You can display one of the four types of dialog boxes as shown below:
Use one of the four standard
dialog box arguments here we
are choosing to open a file.
Note that having chosen a file
to open we must then open it
with the Execute method.
Copyright 2015
Page 36
Chapter 4 - Using File Dialog Boxes
4.3
Customising FileDialogs
There are many ways in which you can change how the file dialog boxes appear, as described
under separate headings below.
Choosing an Initial View
You can choose to display your files/folders as icons, thumbnails, etc initially:
Use this property to set the
initial view (see the possible
options below).
The possible options correspond to some of the
standard Windows file list options.
The standard constants are:
ConstantViewmsoFileDialogViewDetailsDetailsmsoFileDi
alogViewLargeIconsLarge
iconsmsoFileDialogViewListListmsoFileDialogViewPrevie
wPreviewmsoFileDialogViewPropertiesPropertiesmsoFile
DialogViewSmallIconsSmall
iconsmsoFileDialogViewThumbnailThumbnailsmsoFileDia
logViewTilesTiled icons
Wise
Wise Owls
Owls
Hint
Hint
Copyright 2015
Users of Windows Vista may find that changing the initial view of a file
dialog box has no effect when the code runs.
Page 37
Chapter 4 - Using File Dialog Boxes
Setting Filters
Whenever a dialog box appears for opening files, you can control which files you can look for:
Here we have two filters the first
of which is selected by default.
Each filter has a name, a file extension and a position in the list. for our example, the filters are:
Name of filter
Extension
Position
Wise Owl Files
*.wol
All Files
*.*
The code to display the dialog box above would be:
Normally there are lots of pre-set
Excel file extensions (*.xlsx, etc.)
remove these.
Add in our two filters specifying
the name, extension and position of
each.
The default selected filter should be
the first.
Copyright 2015
Page 38
Chapter 4 - Using File Dialog Boxes
Specifying a Default Initial Folder
To do this, use (confusingly) the InitialFileName property:
If you set the InitialFileName
property to be a given path
this is what will be selected
initially in your dialog box.
Changing the Appearance of the Dialog Box
You can change the title and button text in any file dialog box:
The title can be
anything you want.
The default button text
can be customised the
& makes the following
letter a short-cut key (so
here + w would open a
file).
Copyright 2015
Page 39
Chapter 4 - Using File Dialog Boxes
4.4
Processing the File(s)/Folder(s) Picked
There are two types of file dialog box:
Types of dialog box
Examples
Dialog boxes which do something
msoFileDialogOpen opens a file
msoFileDialogSaveAs - saves a file
Dialog boxes which dont
msoFileDialogFilePicker picks a file
msoFileDialogFolderPicker picks a folder
Despite this apparent difference, you can use the Open and SaveAs dialog boxes to choose a file
or folder just dont apply the Execute method afterwards.
Finding Out which File was Chosen
A file dialog box has a SelectedItems() property, giving an array of the files chosen. Also, when
you apply the Show method you can get back one of two values:
Value
What it means
-1
The user clicked on the default button (Open, Save or whatever)
The user chose Cancel or closed the dialog box without doing anything.
You can put all this information together to find out the file a user chose, as in this example:
This command states that a user will only be able
to select a single file.
If the user
chooses a
single file:
this macro will display
this message:
If the dialog box returns 0, the user chose Cancel
and we see this message.
Copyright 2015
Page 40
Chapter 4 - Using File Dialog Boxes
Multiple File Selection
You can set a dialog boxs AllowMultiSelect property to True (the default anyway) to allow
users to use the Ctrl and
keys to select (and possibly open) more than one file:
Ensure that the user can select more than
one file.
If the user chose loop over all the items
chosen and display the name of each file
chosen in the immediate window.
Copyright 2015
Page 41
Chapter 4 - Using File Dialog Boxes
4.5
Other Application Dialog Boxes
Application Dialog Boxes
In Word and Excel you can bring up any built-in dialog box using the Dialogs collection:
This code would show the standard
column width dialog box in Excel:
A selection of the dialog boxes that
you can use in Word 2007.
Wise
Wise Owls
Owls
Hint
Hint
For some reason Access and PowerPoint
do not seem to support this feature.
A Special Case for Excel GetOpenFileName
For some reason Excel has a special way to display the Open File dialog box:
Using GetOpenFileName offers no great advantage of using FileDialogs, and
is best avoided so that you can keep compatibility in your VBA code between
Office applications.
Copyright 2015
Page 42