CommonDialogBox or Common Dialog Control
The Common Dialog control provides a standard set of dialog boxes for operations such as
opening, saving, and printing files, as well as selecting colors and fonts and displaying help.
Common Dialog control: show methods
ShowOpen, ShowSave, ShowPrinter, ShowColor, ShowFont, or ShowHelp.
The Common Dialog control an "Active X" control that must be added to the toolbox via the
Components dialog box, as shown below. This dialog box is accessed via the Project menu,
Components item. Once you check "Microsoft Common Dialog Control 6.0" and click OK,
the control is added to your toolbox
The Common Dialog control is not visible at run-time.
The Components Dialog Box Common Dialog
Control Added to
Toolbox
ShowColor
The ShowColor method is used to display the Colour dialog box.
The Color property is used to determine which colour was selected.
CommonDialog1.ShowColor
Text1.BackColor = CommonDialog1.Color
ShowFont
The ShowFont method is used to display a list of fonts that are available.
CommonDialog1.ShowFont
Text1.FontName = CommonDialog1.FontName
Text1.FontSize = CommonDialog1.FontSize
ShowHelp
The ShowHelp method is used to display a Windows Help File.
The HelpFile and HelpCommand properties must be set in order for the Help file to be
displayed.
It invokes the Windows Help Engine (Winhlp32.exe) to display a help file.
CommonDialog1.HelpFile = "e:\vbaf\helpfile.hlp"
CommonDialog1.ShowHelp
ShowOpen
The ShowOpen method is used to display the dialog to open files. The FileName Property is
used to determine the name of the file the user selected.
The Filter Property is used to determine the types of file to be shown.
Dim pattern As String
For Text file
CommonDialog1.ShowOpen
Msgbox commondialog1.filename
For image file
CommonDialog1.ShowOpen
Image1.Picture = LoadPicture(CommonDialog1.FileName)
ShowPrinter
The ShowPrinter method is used to display the dialog to open files.
The following example uses the ShowPrinter method with the Printer object to set the number
of copies and page orientation.
CommonDialog1.ShowPrinter
Printer.Copies = CommonDialog1.Copies
Printer.Orientation = CommonDialog1.Orientation
ShowSave
The ShowSave method looks exactly the same as the ShowOpen method, except the default
title is "Save As" rather than "Open".
Dim fso As New FileSystemObject ' project -> references -> microsoft scripting runtime
Dim file1 As TextStream
CommonDialog1.ShowSave
Set file1 = fso.CreateTextFile(CommonDialog1.FileName, True)
file1.WriteLine "visual basic common dialog control example"
file1.Close