CommonDialog

Description

The CommonDialog control provides a standard set of dialog boxes for operations such as opening and saving files, setting print options, and selecting colors and fonts.

The control provides an interface to the routines in the dynamic-link library Commdlg.dll.
To create a dialog box using this control, Commdlg.dll must be in your Windows SYSTEM directory. The dialog displayed by the control is determined by the methods of the control.

Syntax

[Set oCdl =] oDlg.CommonDialog

Parameters

Part

Description

Set oCdl =

(optional) Store a reference to the common dialogs control in the variable oCdl. This variable can then be used to access its properties and methods

oDlg

A reference to the WshDialog.Kit automation object (as returned by the WScript or VBScript CreateObject methods)

Properties and Methods

Property

Description

CancelError

(boolean) Returns or sets a value indicating whether an error is generated when the user chooses the Cancel button. When this property is set to True, error number 32755 occurs whenever the user chooses the Cancel button. The default is False (no error is generated)

Color

(long) Returns or sets the selected color (use the RGB function) For this property to return a color in a Color dialog box, the CCRGBInit flag must be set. In the Font dialog box, the CCEffects flag must be set

Copies

Returns or sets a value that determines the number of copies to be printed
For the Print dialog box, this property returns the number of copies entered by the user in the Copies box. If the PDUseDevModeCopies flag is set, this property always returns 1. For the Printer object, multiple copies may or may not be collated, depending on the printer driver. Multiple copies of the entire document or multiple copies of each page may be printed. For printers that don't support collating, set Copies = 1, and then use a loop in code to print multiple copies of the entire document

DefaultExt

Returns or sets the default filename extension for the dialog box. Use this property to specify a default filename extension, such as .txt or .doc. When a file with no extension is saved, the extension specified by this property is automatically appended to the filename

DialogTitle

Returns or sets the string displayed in the title bar of the dialog box

FileName

Returns or sets the path and filename of the selected file(s). Depending on the OS used, the filenames may be delimited by either nulls or spaces (in which case long file names are not supported!). If a single file is selected this property contains the entire path of the file. If multiple files are selected, the first element contains the path and all other elements the selected files

FileTitle

Returns the name (without the path) of the file to open or save. If multiple files are selected (using the OFNAllowMultiSelect flag) or when the OFNNoValidate flag is set, the FileTitle property won't return a value

Filter

Syntax: Filter [=description1|filter1|description2|filter2...]

A filter specifies the type of files that are displayed in the dialog box's file list box. For example, selecting the filter *.txt displays all text files.

Use this property to provide the user with a list of filters that can be selected when the dialog box is displayed.

Use the pipe ( | ) symbol (ASCII 124) to separate the description and filter values. Don't include spaces before or after the pipe symbol, because these spaces will be displayed with the description and filter values.

The following code shows an example of a filter that enables the user to select text files or graphic files that include bitmaps and icons:

Text (*.txt)|*.txt|Pictures (*.bmp;*.ico)|*.bmp;*.ico

When you specify more than one filter for a dialog box, use the FilterIndex property (see below) to determine which filter is displayed as the default.

FilterIndex

(integer) Returns or sets a default filter for an Open or Save As dialog box.
The index for the first defined filter is 1

Flags

(long) Returns or sets additional options for the various dialog boxes

FontName

The font used to display text. The default is determined by the system. Check the glossary for an explanation about setting font properties

FontSize

The size of the font in points. The maximum is 2160 points

FontBold
FontItalic
FontStrikethru
FontUnderline

(boolean) Enable or disable the bold, italic, strikethru or underline style of a font. To use any of these properties with the CommonDialog control, the CFEffects flag must be set

FromPage,
ToPage

(integer) Return or set the values for the From and To text boxes of the Print dialog (valid only when the PageNums flag has been set)

InitDir

Returns or sets the initial directory for an Open or Save As dialog. If this property isn't specified, the current directory is used

Max,
Min

With the Font dialog box these properties determine the smallest and largest font sizes displayed in the Size list box.( the CFLimitSize flag must be set before using these properties)

With the Print dialog box, these properties determine the smallest and largest numbers the user can specify in the From and To text boxes

MaxFileSize

(integer) Returns or sets the maximum size of the filename(s) opened.
The MaxFileSize property allocates memory to store the actual names of the selected file(s). When using the OFNAllowMultiselect flag, you may need to increase the size of this property to allow enough memory for the selected file names. The default value is 256

Orientation

1

Documents are printed in portrait mode

2

Documents are printed in landscape mode

Parent

(read-only) Returns the parent form, object or collection. You can use the parent property to access the properties and methods of an object's parent

PrinterDefault

(boolean) Determines if the user's selections in the Print dialog box are used to change the system's default printer settings

Method

Description

ShowColor

Show Color Dialog Box

ShowFont

Show Font Dialog Box

ShowOpen

Show Open Dialog Box

ShowPrinter

Show Print or Print Options Dialog Box

ShowSave

Show Save As Dialog Box

Example

Option Explicit

Const vbModal = 1

'Some common dialog flags used
Const cAllowMultiselect = &H200  
'Allow selection of multiple files
Const cExplorer = &H80000          
'Use the explorer-like Open dialog
Const cNoChangeDir = &H8          
'Do not change the active directory

Dim oDlg, oFrm, oCtl

'Create the WshDialog.Kit object and store a reference in oDlg
Set oDlg = Wscript.CreateObject("WshDialog.Kit", "oDlg_")

'Add a new form and store a reference to it in the variable oFrm
Set oFrm = oDlg.NewForm("")

'Set the form's caption
oFrm.Caption = "Testing the Common Dialog Control"

'Add a frame, named FRM1, to the form
oFrm.NewFrame "FRM1", 300, 100, 2000, 2200, " Choose a dialog "

'Add optionbuttons to the FRM1 frame for each of the common dialogs
oFrm.NewOptionButton "OPEN", 200, 350, 1700, 255, "Open File Dialog", True, "FRM1"
oFrm.NewOptionButton "SAVE", 200, 700, 1700, 255, "Save As Dialog", False, "FRM1"
oFrm.NewOptionButton "PRINT", 200, 1050, 1700, 255, "Printer Dialog", False, "FRM1"
oFrm.NewOptionButton "COLOR", 200, 1400, 1700, 255, "Color Dialog", False, "FRM1"
oFrm.NewOptionButton "FONT", 200, 1750, 1700, 255, "Font Dialog", False, "FRM1"

'Add an OK button and set it's Default property
Set oCtl = oFrm.NewButton("OK", 2500, 400, 1000, 375, "&OK")
oCtl.Default = True

'Add a CANCEL button and set it's Cancel property
Set oCtl = oFrm.NewButton("CANCEL", 2500, 1000, 1000, 375, "&Cancel")
oCtl.Cancel = True

'Automatically size the form and show it (modally)
oFrm.Autosize

'Enable event handling (callback) for this form
oFrm.CallBack = True

'Show the form
oFrm.Show vbModal

'--------------------------------------------------------------------------------------------------
' oDlg_ClickHandler handles the events sent by the controls
'--------------------------------------------------------------------------------------------------

Sub oDlg_ClickHandler(sForm, sControl)

    Dim oFrm, oCtl, oCdl
    Dim aFiles, X

    
'Get a reference to the form and the control that raised the event
    Set oFrm = oDlg.Frm(sForm)
    Set oCtl = oFrm.Ctl(sControl)

    
'Check which control caused the event
    Select Case UCase(sControl)
    Case "OK"
        
'Get a reference to the common dialog control
        Set oCdl = oDlg.CommonDialog
        
'Initialize some common flags for both the Open and Save dialogs
        oCdl.Flags = oCdl.Flags OR (cAllowMultiselect + cExplorer + cNoChangeDir)
        oCdl.InitDir = "C:\Windows"
        oCdl.FileName = ""
        oCdl.DefaultExt = ".txt"
        oCdl.Filter = "Configuration Files (*.ini,*.cfg)|*.ini;*.cfg|Text Files (*.txt)|*.txt"
        oCdl.FilterIndex = 2
        oCdl.CancelError = False
        
'Check which optionbutton was selected
        Select Case oFrm.GetOptionButton("FRM1")
        Case "OPEN"
            oCdl.DialogTitle = "Pick a file to open ..."
            oCdl.MaxFileSize = 5120        
'Allocate sufficient memory
            oCdl.CancelError = True
            On Error Resume Next
            oCdl.ShowOpen
            Select Case Err.Number
            Case 0
                
'Read the selected file(s) from the FileName property
                
'Depending on the OS the delimiter maybe either nulls or spaces!
                aFiles = Split(oCdl.FileName, Chr(0))
                Select Case Ubound(aFiles)
                Case -1
                    MsgBox "No file(s) selected"
                Case 0
                    
'Only 1 file selected
                    MsgBox aFiles(0)
                Case Else
                    For X = 1 To Ubound(aFiles)
                        
'The directory is at index 0, all others elements are the selected files
                        MsgBox aFiles(0) & "\" & aFiles(X)
                    Next
                End Select
            Case Else
                MsgBox Err.Description
            End Select
        Case "SAVE"
            oCdl.DialogTitle = "Save the file as ..."
            oCdl.ShowSave
            If oCdl.FileName = "" Then
                MsgBox "No filename specified"
            Else
                MsgBox "The file will be saved as : " & oCdl.Filename
            End If
        Case "PRINT"
            oCdl.ShowPrinter
        Case "COLOR"
            oCdl.ShowColor
        Case "FONT"
            oCdl.ShowFont
         Case Else
         End Select
    Case "CANCEL", "*CLOSE"
        
'The Cancel button or closebox was clicked. Dismiss the form (hide it)
        oFrm.Hide
    Case Else
        
'Ignore all other events. Do NOT use oFrm.Hide here,
        
'or any event not handled above will dismiss the form
    End Select

End Sub