# VBForums UtilityBank > UtilityBank - Tutorials >  VB6 FAQ for Common Dialog Control

## Siddharth Rout

*T*his tutorial is about the *Common Dialog Control (CDC)* which provides a standard interface for operations such as opening, saving, printing, selecting colours/fonts and for showing Help using the *Microsoft Windows* dynamic link library COMMDLG.DLL. 

To use a *Common Dialog Control*, add "*Microsoft Common Dialog Control*" from the "*Project*", "*Components*" menu item. See picture below.




This will add the control the the Tool Box as shown below.


The best part is that CDC is visible on the form as a small icon at design-time but will be invisible at run-time.

Like I mentioned above, the CDC can be used for the following operations.
Open File using .ShowOpenSave File using .ShowSaveSetting Color using .ShowColorSetting Font using .ShowFontPrint using .ShowPrinterDisplay Help using .ShowHelp

Lets cover each of them by creating a small Project.

*Setting up the form*
Create a form and add 6 buttons and set their captions as shown in the picture below

Place the CDC in the form as shown aboveAnd lastly put a Rich Text Box. (To use the Rich Text Box Control, add "*Microsoft Rich TextBox Control*" from the "*Project*", "*Components*" menu item.)

*Setting up the code*

Paste the code below and then simply run the form. I have commented the code so you won’t have any difficulty understanding it  :Smilie: 



```
'~~> ShowOpen Example
Private Sub Command1_Click()
    On Error GoTo cancel_error
    
    With CommonDialog1
        '~~> Set the Title of the Dialog Box
        .DialogTitle = "Open"
        '~~> Set the Initial directory
        .InitDir = "C:\"
        '~~> Set the Filter for the files that you want to open
        .Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
        '~~> Displays the Open Dialog Box
        .ShowOpen
    End With
    
    '~~> Load the Rich Text Box with the text file
    RichTextBox1.LoadFile CommonDialog1.FileName
Exit Sub

cancel_error:
     If Err.Number <> vbCancel Then
          MsgBox Err.Description
     End If
End Sub

'~~> ShowSave Example
Private Sub Command2_Click()
    On Error GoTo cancel_error
    
    With CommonDialog1
        '~~> Set the Title of the Dialog Box
        .DialogTitle = "Save"
        '~~> Set the Initial directory
        .InitDir = "C:\"
        '~~> Set the Filter for the files that you want to save as
        .Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
        '~~> Displays the Save Dialog Box
        .ShowSave
    End With
    
    '~~> Save the Rich Text Box contents as a text file
    RichTextBox1.SaveFile CommonDialog1.FileName
Exit Sub

cancel_error:
     If Err.Number <> vbCancel Then
          MsgBox Err.Description
     End If
End Sub

'~~> ShowColor Example
Private Sub Command3_Click()
    On Error GoTo cancel_error
    
    With CommonDialog1
          .DialogTitle = "Color"
          '~~> Show the Color Dialog box
          .ShowColor
     End With
     
     '~~> Set the back color of the Dialogbox
     RichTextBox1.BackColor = CommonDialog1.Color
Exit Sub

cancel_error:
     If Err.Number <> vbCancel Then
          MsgBox Err.Description
     End If
End Sub

'~~> ShowFont Example
Private Sub Command4_Click()
    On Error GoTo cancel_error
    
    With CommonDialog1
        .DialogTitle = "Font"
        
        '~~> Various flags that you can use
        ' cdlCFBoth         ~~> Display Screen's and Printer's fonts
        ' cdlCFEffects      ~~> For Underline, FontStrikethru, Color
        ' cdlCFTTOnly       ~~> Display True Type fonts
        ' cdlCFScreenFonts  ~~> Displays Screen fonts
        ' cdlCFPrinterFonts ~~> Displays Printer fonts
        
        .Flags = cdlCFBoth Or cdlCFEffects
        
        '~~> Show the Font Dialog box
        .ShowFont
    End With
    
    '~~> Select the entire text in the Rich Text Box
    '~~> Comment the below two lines if you want to
    '~~> Manually select words in the Rich Text Box
    RichTextBox1.SelStart = 0
    RichTextBox1.SelLength = Len(RichTextBox1)
    
    '~~> Change Font
    RichTextBox1.SelFontName = CommonDialog1.FontName
    '~~> Change Font Size
    RichTextBox1.SelFontSize = CommonDialog1.FontSize
    '~~> Bold Font
    RichTextBox1.SelBold = CommonDialog1.FontBold
    '~~> Italicize Font if ticked
    RichTextBox1.SelItalic = CommonDialog1.FontItalic
    '~~> Underline if ticked
    RichTextBox1.SelUnderline = CommonDialog1.FontUnderline
    
    '~~> Strike through Font if ticked
    RichTextBox1.SelStrikeThru = CommonDialog1.FontStrikethru
Exit Sub

cancel_error:
     If Err.Number <> vbCancel Then
          MsgBox Err.Description
     End If
End Sub

'~~> ShowPrinter Example
Private Sub Command5_Click()
    On Error GoTo cancel_error
    
    '~~> Show the printer dialog box
    CommonDialog1.ShowPrinter
    
    '~~> Print the text in the Rich Text Box
    Printer.Print RichTextBox1.Text
    Printer.EndDoc
Exit Sub

cancel_error:
     If Err.Number <> vbCancel Then
          MsgBox Err.Description
     End If
End Sub

'~~> ShowHelp Example
Private Sub Command6_Click()
    On Error GoTo cancel_error
    
    '~~> Set the help file
    CommonDialog1.HelpFile = "MyHelp.hlp"
    CommonDialog1.HelpCommand = cdlHelpContents
    '~~> Show the Help File
    CommonDialog1.ShowHelp
Exit Sub

cancel_error:
     If Err.Number <> vbCancel Then
          MsgBox Err.Description
     End If
End Sub
```

I am also attaching the Zipped Project…

Hope this helps…  :wave:

----------


## fafalone

Since I just dealt with this issue myself, I thought I'd add a few things.

If you want the Open/Save dialogs to look like they normally do in Windows programs and not like a Win 3.1 nightmare, you'll need to do the following:

Const cdlOFNExplorer = &H80000

then when setting up the dialog,

.flags = cdlOFNExplorer


Also, to open multiple files:
Const cdlOFNAllowMultiselect = &H200

.flags = cdlOFNExplorer Or cdlOFNAllowMultiselect

(assuming you want the modern dialog too, if not, just the multiselect)

The documentation states that multiple names in .Filename are space delimited; however this is not true. They are null-character delimited.
So, to get at all the names, you'll need

Split(.Filename, vbNullChar)

The resulting array's first value (index 0) will be the path, without the filename, then 1 through ubound will be the names of files selected in that folder. Maybe it's just my system, but after opening multiple files and invoking the dialog again, the default name is set to a part of one of the previous file names, so if that happens just clear it by setting .Filename = "" before .Show... .

For all the flags, see: http://msdn.microsoft.com/en-us/library/aa259317%28v=VS.60%29.aspx

----------


## webadage

Is there a way to pre populate the filename when using the commondialog to save a file. Here's what I mean...

I want the file name to be a predetermined sequential number based on the date and time, IE 20220421 1422. I've written the code to create that name already. I want to, when I select SaveAs, open the savefile side of the commondialog and POPULATE that filename. I've searched and searched and no one seems to be talking about this particular aspect. Is it possible and if so... can you share some code examples? Thanks in advance.

Okay... never mind. I finally got it to work. I was using examples I found here that didn't seem to work and.... duh... I had the KEY code in the wrong position. 

getDate 'this procedure creates the new filename by formatting the DATE and TIME information sequentially as yr mo da hr min sec
newFN = newFN & ".rtf"
CommonDialog1.Flags = cdlOFNOverwritePrompt
CommonDialog1.Filter = "Rich Text Format files|*.rtf"
CommonDialog1.InitDir = App.Path & "\Entries"
CommonDialog1.FileName = newFN
CommonDialog1.ShowSave 'when the save dialog opens, the filename I created is already in place
rtfData.SaveFile CommonDialog1.FileName, rtfRTF
datChange = 0 'this line resets the change variable indicating no changes have been made since the last save.

----------

