Tuesday, June 19, 2007

Visual Basic Common Dialog

Opening Files with Common Dialog

Common Dialog not only replaces three controls (Drive, Directory and FileList), but also is easier to program. It is supported in Visual Basic and VBA as well. The new VB.NET has the same functionality in the OpenFileDialog class

Let us have a small form created for explaining CommonDialog. Let us have a small form with a Text Box and a Command Button. On Clicking the Command Button, the selected file should be displayed in the Text Box

Sample Form:



To use the CommonDialog you need to include the component to your project. You can do so as follows:





Once The component is included, the CommonDialog will be displayed in the ToolBox




Drag the CommonDialog to the form. You will see a small rectangle there. CommonDialog is visible in the Design time only (it is not visible during runtime)




Add the following code to show the CommonDialog box and show the selected file in the text box


Private Sub Command1_Click()

CommonDialog1.DialogTitle = "Select the File..."

CommonDialog1.Flags = cdlOFNFileMustExist

CommonDialog1.Filter = "Microsoft Excel Workbooks (*.xls)*.xls"

CommonDialog1.ShowOpen

If Len(CommonDialog1.FileName) <> 0 Then

Text1.Text = CommonDialog1.FileName

End If






You can restrict the type of files to be selected using the filter Command. Some common filters are




Selecting Microsoft Word Documents
CommonDialog1.Filter = "Microsoft Word Documents (*.doc)*.docMicrosoft Word Documents (*.rtf)*.rtf"

Selecting Image Files
CommonDialog1.Filter = "Image Files(*.BMP;*.JPG;*.GIF)*.BMP;*.JPG;*.GIF"

Selecting Microsoft Word Documents (Including RTF Files)
CommonDialog1.Filter = "Microsoft Word Documents (*.doc;*.rtf)*.doc;*.rtf"

Selecting Microsoft Excel Workbooks
CommonDialog1.Filter = "Microsoft Excel Workbooks (*.xls)*.xls"

Selecting Excel Addins
CommonDialog1.Filter = "Microsoft Excel Addins (*.xla;*.xll)*.xla;*.xll"

Selecting Any files
CommonDialog1.Filter = "All files (*.*)*.*"

Selecting Text files
CommonDialog1.Filter = "Text files (*.txt)*.txt"

Selecting ASCII files
CommonDialog1.Filter = "ASCII files (*.txt;*.log)*.txt;*.log"

See also:

OpenFileDialog in Visual Basic .Net

Search and Open Files using Excel VBA (FileSearch)

Open Excel Files - Open Dialog - GetOpenFilename Method

Selecting a Folder in VB.Net

Browse a Folder / Select a Folder Thru Shell

SaveAs Dialog - Controlled Save

14 comments:

  1. Rafael8:36 AM

    Thank you very much! Pretty simple and exactly what I wanted!!!

    You saved my life ;)

    ReplyDelete
  2. Anonymous10:50 AM

    i try opening a picture to a picturebox..but is have an error.. i try to change the .filename to .object still error pls help me.

    ReplyDelete
  3. WOW! After trying to search the web for months, i finally found what i need! Thank you so much bro!

    ReplyDelete
  4. Anonymous8:24 AM

    So, how do you put multiple filetypes in the selection box?

    ReplyDelete
  5. Anonymous8:27 AM

    answer ..

    CommonDialog1.Filter = "Text Files (*.txt)|*.txt|Word Documents"(*.doc)|*.doc|Rich Text Files (*.rtf)|*.rtf|All Files|*.*"

    ReplyDelete
  6. Anonymous10:09 PM

    Thank you very much! xactly what I wanted!!!

    ReplyDelete
  7. Anonymous1:20 AM

    To correctly filter the file you should add the '|'. Meaning

    CommonDialog1.Filter = "Text files (*.txt)|*.txt" instead of

    CommonDialog1.Filter = "Text files (*.txt)*.txt"

    ReplyDelete
  8. Anonymous11:33 AM

    I used the dialog once, with OK, and it was OK.
    A second call for it, with a "Cancel" button pressed, returns the first name, no sign the user has pressed "Cancel". Any suggestions ?

    ReplyDelete
  9. Anonymous11:39 AM

    I got it! I need to set CancelError to TRUE !
    Thanks

    ReplyDelete
  10. """"To correctly filter the file you should add the '|'. Meaning

    CommonDialog1.Filter = "Text files (*.txt)|*.txt" instead of

    CommonDialog1.Filter = "Text files (*.txt)*.txt""""

    Yes this works and corrects showing all files!

    ReplyDelete
    Replies
    1. Thanks. I was wondering why my filter doesn't work. This solves the problem.

      Delete
  11. rcchua11:11 PM

    How to prompt the user when using the
    CommonDialog saving features if a filename
    is empty or ""?

    since when I click the save button on the
    CommonDialog it does not do anything. is the
    filename required to save a file?

    ReplyDelete
  12. Anonymous3:57 PM

    Hi All. Please how can I get the Visual Basic software? VB6 or any other - full version. Please advise. Thanks in advance.

    ReplyDelete
  13. Anonymous1:38 AM

    Hi, how to filter vsd files?

    ReplyDelete

StumbleUpon
Share on Facebook
Related Posts Plugin for WordPress, Blogger...
Download Windows Live Toolbar and personalize your Web experience! Add custom buttons to get the information you care about most.