February 24, 2021
Hot Topics:

Using the Common Dialog Open/Save Boxes

  • By John Percival
  • Send Email »
  • More Articles »

This control contains the interface for the open/save dialogs, as well as several others, namely colour and font pickers, printer setup and an interface for Winhelp. Using the control is as simple as placing the control on a form and calling a method. Let's do that now:

  1. Place a Common Dialog control, CommonDialog1, on the form.
  2. Place a button, Command1, on the form. In the click event add the following code:
Private Sub Command1_Click()
MsgBox "You chose: " & CommonDialog1.filename
End Sub

When you run this, you will see that the CommonDialog control is not visible. However, it is sitting in the background, and when you click the button, the open dialog is shown. If you had wanted to show the save dialog, you would have called CommonDialog1.ShowSave, rather than .ShowOpen. When the dialog is closed, the filename that you chose is placed in the filename property of the control. You can also retrieve the filename and path separately very easily. The filename on its own is stored in the filetitle property. With a little juggling of strings, you can find out the path. Add the following code to Command1_Click:

MsgBox "Filename: " & _
MsgBox "Path: " & Left$(CommonDialog1.filename, _
  Len(CommonDialog1.filename) - Len(CommonDialog1.FileTitle))

But what if the user does not select a file and clicks on cancel? Here we can use a property called 'CancelError' to tell the control to raise a trappable error when the user selects cancel. You can then trap this error, and take due action. Modify the code in Command1_Click to read:

On Error Resume Next ' Set up error handler
CommonDialog1.CancelError = True
If Err.Number = cdlCancel Then
MsgBox "You clicked cancel!"
MsgBox "You chose: " & CommonDialog1.filename
MsgBox "Filename: " & CommonDialog1.FileTitle
MsgBox "Path: " & Left$(CommonDialog1.filename, _
Len(CommonDialog1.filename) - Len(CommonDialog1.FileTitle))
End If
On Error GoTo 0 ' Turn off error handler

Now when you click cancel, error 32755(cdlCancel) is raised. The error handler catches it and resumes the next statement. The next statement checks the error object to see if the error has occurred; if it has, it lets you know, otherwise, it operates as normal. In your applications, you might consider asking the user to confirm that they wanted to click cancel and to give them the option to try again.

You can set which directory the dialog starts in, using the InitDir property. To demonstrate this, draw a DirListBox on the form, called Dir1. Add the following line before the call to CommonDialog1.ShowOpen:

CommonDialog1.InitDir = Dir1.Path

In your applications, you could allow the user to specify a default location to open in. You may also only want the user to be able to specify certain types of files. You can do this by using a filter. A filter consists of a list of description and filenames to use as filters. It is in the form:


You can have as many filters as you like, and if you want to specify more than one filename, separate it with a semicolon. Each description and filter is separated by a pipe (ASCII 124). For example, to allow the user to select certain programs, you might use the following filter:

"Batch Files (*.bat)|*.bat|Files beginning with a (a*.*)|a*.*|Batch files beginning with a (a*.bat)|a*.bat|Executables (*.exe;*.com)|*.exe;*.com|All files (*.*)|*.*"

By setting FilterIndex, you can choose which filter is selected as default. The first filter is 1, the second is 2, etc. When the dialog box is closed, filterindex contains the filter that the user had chosen.

Page 2 of 5

This article was originally published on November 20, 2002

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date