Visual Basic: CommonDialog Control

CommonDialog Control (Open, Save As Dialogs) Example

The following example shows the Open dialog then displays the selected filename in a message box:

  Private Sub Command1_Click()
  ' Set CancelError is True
  CommonDialog1.CancelError = True
  On Error GoTo ErrHandler
  ' Set flags
  CommonDialog1.Flags = cdlOFNHideReadOnly
  ' Set filters
  CommonDialog1.Filter = "All Files (*.*)|*.*|Text Files" & _
  "(*.txt)|*.txt|Batch Files (*.bat)|*.bat"
  ' Specify default filter
  CommonDialog1.FilterIndex = 2
  ' Display the Open dialog box
  CommonDialog1.ShowOpen
  ' Display name of selected file
  MsgBox CommonDialog1.filename
  Exit Sub
  
ErrHandler:
  'User pressed the Cancel button
  Exit Sub
End Sub