This 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 .ShowOpen
- Save File using .ShowSave
- Setting Color using .ShowColor
- Setting Font using .ShowFont
- Print using .ShowPrinter
- Display 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 above
- And 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 
Code:
'~~> 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…