Skip to content

CommonDialog Control

Provides a wrapper class for Windows standard dialogs (Open, Save, Color, Font, Print, Help, Page Setup, Folder Browser, Find, Replace).

Enumerations

CdlErrorConstants

ConstantValueDescription
CdlCancel32755User selected "Cancel"
CdlBufferTooSmall20476File name buffer is too small
CdlInvalidFileName20477Invalid file name
CdlSubclassFailure20478Subclassing failed
CdlMaxLessThanMin24573Minimum value is greater than maximum
CdlNoFonts24574No fonts available
CdlPrinterNotFound28660Printer not found
CdlCreateICFailure28661Failed to create information context
CdlDndmMismatch28662DEVMODE mismatch
CdlNoDefaultPrn28663No default printer
CdlNoDevices28664No print devices
CdlInitFailure28665Print dialog initialization failed
CdlGetDevModeFail28666Failed to get DEVMODE
CdlLoadDrvFailure28667Failed to load printer driver
CdlRetDefFailure28668Failed to return default DEVMODE
CdlParseFailure28669Parse failure
CdlHelp32751Help request
CdlBufferLengthZero36848Buffer length is zero

CdlPRORConstants

ConstantValueDescription
CdlPRORPortraitvbPRORPortraitPortrait orientation
CdlPRORLandscapevbPRORLandscapeLandscape orientation

CdlPRPSConstants

ConstantValueDescription
CdlPRPSLettervbPRPSLetterLetter
CdlPRPSLetterSmallvbPRPSLetterSmallLetter Small
CdlPRPSTabloidvbPRPSTabloidTabloid
CdlPRPSLedgervbPRPSLedgerLedger
CdlPRPSLegalvbPRPSLegalLegal
CdlPRPSStatementvbPRPSStatementStatement
CdlPRPSExecutivevbPRPSExecutiveExecutive
CdlPRPSA3vbPRPSA3A3
CdlPRPSA4vbPRPSA4A4
CdlPRPSA4SmallvbPRPSA4SmallA4 Small
CdlPRPSA5vbPRPSA5A5
CdlPRPSB4vbPRPSB4B4
CdlPRPSB5vbPRPSB5B5
CdlPRPSFoliovbPRPSFolioFolio
CdlPRPSQuartovbPRPSQuartoQuarto
CdlPRPS10x14vbPRPS10x1410x14
CdlPRPS11x17vbPRPS11x1711x17
CdlPRPSNotevbPRPSNoteNote
CdlPRPSEnv9vbPRPSEnv9Envelope #9
CdlPRPSEnv10vbPRPSEnv10Envelope #10
CdlPRPSEnv11vbPRPSEnv11Envelope #11
CdlPRPSEnv12vbPRPSEnv12Envelope #12
CdlPRPSEnv14vbPRPSEnv14Envelope #14
CdlPRPSCSheetvbPRPSCSheetC Sheet
CdlPRPSDSheetvbPRPSDSheetD Sheet
CdlPRPSESheetvbPRPSESheetE Sheet
CdlPRPSEnvDLvbPRPSEnvDLEnvelope DL
CdlPRPSEnvC5vbPRPSEnvC5Envelope C5
CdlPRPSEnvC3vbPRPSEnvC3Envelope C3
CdlPRPSEnvC4vbPRPSEnvC4Envelope C4
CdlPRPSEnvC6vbPRPSEnvC6Envelope C6
CdlPRPSEnvC65vbPRPSEnvC65Envelope C65
CdlPRPSEnvB4vbPRPSEnvB4Envelope B4
CdlPRPSEnvB5vbPRPSEnvB5Envelope B5
CdlPRPSEnvB6vbPRPSEnvB6Envelope B6
CdlPRPSEnvItalyvbPRPSEnvItalyEnvelope Italy
CdlPRPSEnvMonarchvbPRPSEnvMonarchEnvelope Monarch
CdlPRPSEnvPersonalvbPRPSEnvPersonalEnvelope Personal
CdlPRPSFanfoldUSvbPRPSFanfoldUSFanfold US
CdlPRPSFanfoldStdGermanvbPRPSFanfoldStdGermanFanfold Std German
CdlPRPSFanfoldLglGermanvbPRPSFanfoldLglGermanFanfold Lgl German
CdlPRPSUservbPRPSUserUser-defined

CdlPRBNConstants

ConstantValueDescription
CdlPRBNUppervbPRBNUpperUpper paper bin
CdlPRBNLowervbPRBNLowerLower paper bin
CdlPRBNMiddlevbPRBNMiddleMiddle paper bin
CdlPRBNManualvbPRBNManualManual paper feed
CdlPRBNEnvelopevbPRBNEnvelopeEnvelope bin
CdlPRBNEnvManualvbPRBNEnvManualEnvelope manual feed
CdlPRBNAutovbPRBNAutoAuto feed
CdlPRBNTractorvbPRBNTractorTractor feed
CdlPRBNSmallFmtvbPRBNSmallFmtSmall format bin
CdlPRBNLargeFmtvbPRBNLargeFmtLarge format bin
CdlPRBNLargeCapacityvbPRBNLargeCapacityLarge capacity bin
CdlPRBNCassettevbPRBNCassetteCassette bin

CdlPRPQConstants

ConstantValueDescription
CdlPRPQHighvbPRPQHighHigh quality
CdlPRPQMediumvbPRPQMediumMedium quality
CdlPRPQLowvbPRPQLowLow quality
CdlPRPQDraftvbPRPQDraftDraft quality

CdlPRCMConstants

ConstantValueDescription
CdlPRCMMonochromevbPRCMMonochromeMonochrome printing
CdlPRCMColorvbPRCMColorColor printing

CdlPRDPConstants

ConstantValueDescription
CdlPRDPSimplexvbPRDPSimplexSingle-sided printing
CdlPRDPHorizontalvbPRDPHorizontalDouble-sided horizontal flip
CdlPRDPVerticalvbPRDPVerticalDouble-sided vertical flip

CdlOFNConstants

ConstantValueDescription
CdlOFNReadOnly&H1Show read-only checkbox
CdlOFNOverwritePrompt&H2Prompt before overwriting file
CdlOFNHideReadOnly&H4Hide read-only checkbox
CdlOFNNoChangeDir&H8Do not change current directory
CdlOFNHelpButton&H10Show help button
CdlOFNNoValidate&H100Do not validate file name
CdlOFNAllowMultiSelect&H200Allow multiple selections
CdlOFNExtensionDifferent&H400Extension is different
CdlOFNPathMustExist&H800Path must exist
CdlOFNFileMustExist&H1000File must exist
CdlOFNCreatePrompt&H2000Prompt to create file
CdlOFNShareAware&H4000Ignore sharing errors
CdlOFNNoReadOnlyReturn&H8000&Do not return read-only files
CdlOFNNoNetworkButton&H20000Hide network button
CdlOFNExplorer&H80000Use Explorer-style dialog
CdlOFNNoDereferenceLinks&H100000Do not dereference shortcuts
CdlOFNDontAddToRecent&H2000000Do not add to recent files
CdlOFNForcesShowHidden&H10000000Show hidden files

CdlOFNShareViResultConstants

ConstantValueDescription
CdlOFNShareViResultWarn&H0Warn on sharing violation
CdlOFNShareViResultNoWarn&H1Do not warn on sharing violation
CdlOFNShareViResultFallThrough&H2Ignore sharing violation

CdlCCConstants

ConstantValueDescription
CdlCCRGBInit&H1Use initial color
CdlCCFullOpen&H2Fully open the dialog
CdlCCPreventFullOpen&H4Prevent full open
CdlCCHelpButton&H8Show help button
CdlCCSolidColor&H80Solid colors only
CdlCCAnyColor&H100Any color

CdlCFConstants

ConstantValueDescription
CdlCFScreenFonts&H1Screen fonts
CdlCFPrinterFonts&H2Printer fonts
CdlCFHelpButton&H4Show help button
CdlCFEffects&H100Enable effects options
CdlCFApply&H200Enable Apply button
CdlCFScriptsOnly&H400Script fonts only
CdlCFNoVectorFonts&H800Exclude vector fonts
CdlCFLimitSize&H2000Limit font size
CdlCFFixedPitchOnly&H4000Fixed-pitch fonts only
CdlCFForceFontExist&H10000Font must exist
CdlCFScalableOnly&H20000Scalable fonts only
CdlCFTTOnly&H40000TrueType fonts only
CdlCFNoFaceSel&H80000No font name selected
CdlCFNoStyleSel&H100000No style selected
CdlCFNoSizeSel&H200000No size selected
CdlCFSelectScript&H400000Select script
CdlCFNoScriptSel&H800000No script selected
CdlCFNoVertFonts&H1000000Exclude vertical fonts

CdlPDConstants

ConstantValueDescription
CdlPDAllPages&H0All pages
CdlPDSelection&H1Selection
CdlPDPageNums&H2Page range
CdlPDNoSelection&H4Disable selection
CdlPDNoPageNums&H8Disable page range
CdlPDCollate&H10Collate copies
CdlPDPrintToFile&H20Print to file
CdlPDPrintSetup&H40Show print setup
CdlPDNoWarning&H80No warning
CdlPDReturnDC&H100Return device context
CdlPDReturnIC&H200Return information context
CdlPDReturnDefault&H400Return default printer
CdlPDHelpButton&H800Show help button
CdlPDUseDevModeCopies&H40000Use DEVMODE copy count
CdlPDUseDevModeCopiesAndCollate&H40000Use DEVMODE copies and collate
CdlPDDisablePrintToFile&H80000Disable print to file
CdlPDCurrentPage&H400000Current page
CdlPDHidePrintToFile&H100000Hide print to file
CdlPDNoNetworkButton&H200000Hide network button
CdlPDNoCurrentPage&H800000Disable current page

CdlPDResultConstants

ConstantValueDescription
CdlPDResultCancel&H0User cancelled
CdlPDResultPrint&H1User chose print
CdlPDResultApply&H2User chose apply

CdlHelpConstants

ConstantValueDescription
CdlHelpContext&H1Context help
CdlHelpQuit&H2Quit help
CdlHelpIndex&H3Help index
CdlHelpContents&H3Help contents
CdlHelpHelpOnHelp&H4Help on help
CdlHelpSetIndex&H5Set help index
CdlHelpSetContents&H5Set help contents
CdlHelpContextPopup&H8Context popup help
CdlHelpForceFile&H9Force help file
CdlHelpKey&H101Keyword help
CdlHelpCommandHelp&H102Command help
CdlHelpPartialKey&H105Partial keyword help

CdlPSDConstants

ConstantValueDescription
CdlPSDDefaultMinMargins&H0Default minimum margins
CdlPSDMinMargins&H1Allow setting minimum margins
CdlPSDMargins&H2Allow setting margins
CdlPSDInThousandthsOfInches&H4In thousandths of inches
CdlPSDInHundredthsOfMillimeters&H8In hundredths of millimeters
CdlPSDDisableMargins&H10Disable margins
CdlPSDDisablePrinter&H20Disable printer button
CdlPSDNoWarning&H80No warning
CdlPSDDisableOrientation&H100Disable orientation
CdlPSDDisablePaper&H200Disable paper
CdlPSDReturnDefault&H400Return default settings
CdlPSDHelpButton&H800Show help button
CdlPSDDisablePagePainting&H80000Disable page painting
CdlPSDNoNetworkButton&H200000Hide network button

CdlBIFConstants

ConstantValueDescription
CdlBIFReturnOnlyFSDirs&H1Return file system directories only
CdlBIFDontGoBelowDomain&H2Do not go below domain
CdlBIFStatusText&H4Include status text
CdlBIFReturnFSAncestors&H8Return file system ancestors
CdlBIFEditBox&H10Include edit box
CdlBIFValidate&H20Validate input
CdlBIFNewDialogStyle&H40New dialog style
CdlBIFBrowseIncludeURLs&H80Include URLs
CdlBIFUseNewUI&H50Use new UI
CdlBIFUAHint&H100User hint
CdlBIFNoNewFolderButton&H200Hide new folder button
CdlBIFNoTranslateTargets&H400Do not translate targets
CdlBIFBrowseForComputer&H1000Browse for computer only
CdlBIFBrowseForPrinter&H2000Browse for printer only
CdlBIFBrowseIncludeFiles&H4000Include files
CdlBIFShareable&H8000&Shareable
CdlBIFBrowseFileJunctions&H10000Browse file junctions

CdlFRConstants

ConstantValueDescription
CdlFRDown&H1Search downward
CdlFRWholeWord&H2Match whole word
CdlFRMatchCase&H4Case-sensitive
CdlFRFindNext&H8Find next
CdlFRReplace&H10Replace
CdlFRReplaceAll&H20Replace all
CdlFRHelpButton&H80Show help button
CdlFRNoUpDown&H400Disable direction selection
CdlFRNoMatchCase&H800Disable case selection
CdlFRNoWholeWord&H1000Disable whole word selection
CdlFRHideUpDown&H4000Hide direction selection
CdlFRHideMatchCase&H8000&Hide case selection
CdlFRHideWholeWord&H10000Hide whole word selection

Properties

Object

vb
Property Get Object() As Object

Returns the instance of the object itself.

CancelError

vb
Property Get/Let CancelError() As Boolean

Indicates whether an error is raised when the user selects "Cancel".

HookEvents

vb
Property Get/Let HookEvents() As Boolean

Indicates whether the dialog can raise events that require hook callbacks.

Tag

vb
Property Get/Let Tag() As String

Stores additional data needed by the program.

hDC

vb
Property Get hDC() As LongPtr

Returns the device context handle (read-only).

Flags

vb
Property Get/Let Flags() As Long

Returns/sets the dialog option flags.

DialogTitle

vb
Property Get/Let DialogTitle() As String

Sets the string displayed in the dialog title bar.

MaxFileSize

vb
Property Get/Let MaxFileSize() As Long

Returns/sets the maximum size of the open file name.

FileName

vb
Property Get/Let FileName() As String

Returns/sets the path and file name of the selected file.

FileTitle

vb
Property Get FileTitle() As String

Returns the file name of the selected file (without path, read-only).

FileOffset

vb
Property Get FileOffset() As Integer

Returns the zero-based offset from the beginning of the path to the file name (read-only).

Filter

vb
Property Get/Let Filter() As String

Returns/sets the filter displayed in the dialog type list box.

FilterIndex

vb
Property Get/Let FilterIndex() As Long

Returns/sets the default filter index.

InitDir

vb
Property Get/Let InitDir() As String

Returns/sets the initial file directory.

DefaultExt

vb
Property Get/Let DefaultExt() As String

Returns/sets the default file extension.

Color

vb
Property Get/Let Color() As Long

Returns/sets the selected color.

CustomColors

vb
Property Get/Let CustomColors() As Variant

Returns/sets the custom colors available for user selection.

FontName

vb
Property Get/Let FontName() As String

Returns/sets the font name.

FontSize

vb
Property Get/Let FontSize() As Single

Returns/sets the font size (in points).

FontBold

vb
Property Get/Let FontBold() As Boolean

Returns/sets the bold font style.

FontItalic

vb
Property Get/Let FontItalic() As Boolean

Returns/sets the italic font style.

FontStrikethru

vb
Property Get/Let FontStrikethru() As Boolean

Returns/sets the strikethrough font style.

FontUnderline

vb
Property Get/Let FontUnderline() As Boolean

Returns/sets the underline font style.

FontCharset

vb
Property Get/Let FontCharset() As Integer

Returns/sets the font character set.

FontWeight

vb
Property Get/Let FontWeight() As Integer

Returns/sets the font weight (0=Don'tCare, 100=Thin, 200=ExtraLight, 300=Light, 400=Normal, 500=Medium, 600=SemiBold, 700=Bold, 800=ExtraBold, 900=Heavy).

Min

vb
Property Get/Let Min() As Long

Returns/sets the minimum font size (Font dialog) or minimum print page range (Print dialog).

Max

vb
Property Get/Let Max() As Long

Returns/sets the maximum font size (Font dialog) or maximum print page range (Print dialog).

FromPage

vb
Property Get/Let FromPage() As Long

Returns/sets the starting print page.

ToPage

vb
Property Get/Let ToPage() As Long

Returns/sets the ending print page.

Orientation

vb
Property Get/Let Orientation() As CdlPRORConstants

Returns/sets the print orientation.

PaperSize

vb
Property Get/Let PaperSize() As CdlPRPSConstants

Returns/sets the print paper size.

Copies

vb
Property Get/Let Copies() As Integer

Returns/sets the number of print copies.

PaperBin

vb
Property Get/Let PaperBin() As CdlPRBNConstants

Returns/sets the default paper bin.

PrintQuality

vb
Property Get/Let PrintQuality() As CdlPRPQConstants

Returns/sets the print resolution.

ColorMode

vb
Property Get/Let ColorMode() As CdlPRCMConstants

Returns/sets the printer color mode.

Duplex

vb
Property Get/Let Duplex() As CdlPRDPConstants

Returns/sets the duplex printing mode.

PrinterDefault

vb
Property Get/Let PrinterDefault() As Boolean

Returns/sets whether the user selection changes the default printer.

PrinterDefaultInit

vb
Property Get/Let PrinterDefaultInit() As Boolean

Returns/sets whether to always initialize the default printer.

PrinterDriver

vb
Property Get/Let PrinterDriver() As String

Returns/sets the non-default printer driver name.

PrinterName

vb
Property Get/Let PrinterName() As String

Returns/sets the non-default printer device name.

PrinterPort

vb
Property Get/Let PrinterPort() As String

Returns/sets the non-default printer port name.

HelpFile

vb
Property Get/Let HelpFile() As String

Returns/sets the help file name associated with the project.

HelpCommand

vb
Property Get/Let HelpCommand() As CdlHelpConstants

Returns/sets the online help type.

HelpContext

vb
Property Get/Let HelpContext() As LongPtr

Returns/sets the context ID for the help topic.

HelpKey

vb
Property Get/Let HelpKey() As String

Returns/sets the keyword that identifies the help topic.

PageLeftMargin

vb
Property Get/Let PageLeftMargin() As Long

Returns/sets the left margin of the paper (device units).

PageTopMargin

vb
Property Get/Let PageTopMargin() As Long

Returns/sets the top margin of the paper (device units).

PageRightMargin

vb
Property Get/Let PageRightMargin() As Long

Returns/sets the right margin of the paper (device units).

PageBottomMargin

vb
Property Get/Let PageBottomMargin() As Long

Returns/sets the bottom margin of the paper (device units).

PageLeftMinMargin

vb
Property Get/Let PageLeftMinMargin() As Long

Returns/sets the minimum left margin of the paper (device units).

PageTopMinMargin

vb
Property Get/Let PageTopMinMargin() As Long

Returns/sets the minimum top margin of the paper (device units).

PageRightMinMargin

vb
Property Get/Let PageRightMinMargin() As Long

Returns/sets the minimum right margin of the paper (device units).

PageBottomMinMargin

vb
Property Get/Let PageBottomMinMargin() As Long

Returns/sets the minimum bottom margin of the paper (device units).

RootFolder

vb
Property Get/Let RootFolder() As Variant

Returns/sets the root folder for the folder browser dialog.

FindWhat

vb
Property Get/Let FindWhat() As String

Returns/sets the search string for the Find dialog.

ReplaceWith

vb
Property Get/Let ReplaceWith() As String

Returns/sets the replacement string for the Replace dialog.

Action

vb
Property Let Action() As Integer

Sets the type of dialog to display (write-only, 1=Open, 2=Save, 3=Color, 4=Font, 5=Print, 6=Help, 7=Page Setup, 8=Folder Browser, 9=Find, 10=Replace).

Methods

ShowOpen

vb
Public Function ShowOpen() As Boolean

Displays the Open dialog. Returns True on success.

ShowSave

vb
Public Function ShowSave() As Boolean

Displays the Save dialog. Returns True on success.

ShowColor

vb
Public Function ShowColor() As Boolean

Displays the Color dialog. Returns True on success.

ShowFont

vb
Public Function ShowFont() As Boolean

Displays the Font dialog. Returns True on success.

ShowPrinter

vb
Public Function ShowPrinter() As Boolean

Displays the Print dialog. Returns True on success.

ShowPrinterEx

vb
Public Function ShowPrinterEx() As Boolean

Displays the extended Print dialog (PrintDlgEx). Returns True on success.

ShowHelp

vb
Public Sub ShowHelp()

Displays help.

ShowPageSetup

vb
Public Function ShowPageSetup() As Boolean

Displays the Page Setup dialog. Returns True on success.

ShowFolderBrowser

vb
Public Function ShowFolderBrowser() As Boolean

Displays the Folder Browser dialog. Returns True on success.

ShowFind

vb
Public Function ShowFind() As Boolean

Displays the Find dialog. Returns True on success.

ShowReplace

vb
Public Function ShowReplace() As Boolean

Displays the Replace dialog. Returns True on success.

Events

InitDialog

vb
Public Event InitDialog(ByVal Action As Integer, ByVal hDlg As Long)

Occurs when the dialog has completed initialization.

Help

vb
Public Event Help(ByRef Handled As Boolean, ByVal Action As Integer, ByVal hDlg As Long)

Occurs when the user clicks the help button in the dialog.

FileShareViolation

vb
Public Event FileShareViolation(ByVal FileName As String, ByRef Result As CdlOFNShareViResultConstants, ByVal hDlg As Long)

Occurs when the user clicks OK in the Open or Save dialog and a network sharing violation occurs.

FileValidate

vb
Public Event FileValidate(ByVal FileName As String, ByVal FileTitle As String, ByVal FileOffset As Integer, ByRef Cancel As Boolean, ByVal hDlg As Long)

Occurs when the user clicks OK in the Open or Save dialog.

ColorValidate

vb
Public Event ColorValidate(ByRef RGBColor As Long, ByRef Cancel As Boolean, ByVal hDlg As Long)

Occurs when the user clicks OK in the Color dialog.

FontApply

vb
Public Event FontApply(ByVal Flags As Long, ByVal FontName As String, ByVal FontSize As Single, ByVal FontBold As Boolean, ByVal FontItalic As Boolean, ByVal FontStrikethru As Boolean, ByVal FontUnderline As Boolean, ByVal FontCharset As Integer, ByVal RGBColor As Long, ByVal hDlg As Long)

Occurs when the user clicks the "Apply" button in the Font dialog.

FolderBrowserValidateFailed

vb
Public Event FolderBrowserValidateFailed(ByVal Text As String, ByRef Cancel As Boolean, ByVal hDlg As Long)

Occurs when the user enters an invalid name in the Folder Browser dialog.

FindNext

vb
Public Event FindNext()

Occurs when the user clicks the "Find Next" button in the Find or Replace dialog.

Replace

vb
Public Event Replace()

Occurs when the user clicks the "Replace" button in the Replace dialog.

ReplaceAll

vb
Public Event ReplaceAll()

Occurs when the user clicks the "Replace All" button in the Replace dialog.

Code Examples

Basic Usage

vb
Private Sub cmdOpen_Click()
    Dim dlg As CommonDialog
    Set dlg = New CommonDialog
    
    dlg.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
    dlg.FilterIndex = 1
    dlg.CancelError = True
    
    On Error GoTo Cancelled
    If dlg.ShowOpen() Then
        MsgBox "Selected: " & dlg.FileName
    End If
    Exit Sub
Cancelled:
    If Err.Number = CdlCancel Then Exit Sub
    MsgBox "Error: " & Err.Description
End Sub

Using Event Hooks

vb
Private WithEvents dlg As CommonDialog

Private Sub cmdFont_Click()
    Set dlg = New CommonDialog
    dlg.HookEvents = True
    dlg.Flags = CdlCFScreenFonts Or CdlCFEffects Or CdlCFLimitSize
    dlg.Min = 8
    dlg.Max = 72
    dlg.ShowFont
End Sub

Private Sub dlg_FontApply(ByVal Flags As Long, ByVal FontName As String, _
    ByVal FontSize As Single, ByVal FontBold As Boolean, ByVal FontItalic As Boolean, _
    ByVal FontStrikethru As Boolean, ByVal FontUnderline As Boolean, _
    ByVal FontCharset As Integer, ByVal RGBColor As Long, ByVal hDlg As Long)
    Me.Font.Name = FontName
    Me.Font.Size = FontSize
    Me.Font.Bold = FontBold
    Me.Font.Italic = FontItalic
End Sub

VB6 and LOGO copyright of Microsoft Corporation