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
Tuesday, June 19, 2007
Download Windows Live Toolbar and personalize your Web experience! Add custom buttons to get the information you care about most.
Thank you very much! Pretty simple and exactly what I wanted!!!
ReplyDeleteYou saved my life ;)
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.
ReplyDeleteWOW! After trying to search the web for months, i finally found what i need! Thank you so much bro!
ReplyDeleteSo, how do you put multiple filetypes in the selection box?
ReplyDeleteanswer ..
ReplyDeleteCommonDialog1.Filter = "Text Files (*.txt)|*.txt|Word Documents"(*.doc)|*.doc|Rich Text Files (*.rtf)|*.rtf|All Files|*.*"
Thank you very much! xactly what I wanted!!!
ReplyDeleteTo correctly filter the file you should add the '|'. Meaning
ReplyDeleteCommonDialog1.Filter = "Text files (*.txt)|*.txt" instead of
CommonDialog1.Filter = "Text files (*.txt)*.txt"
I used the dialog once, with OK, and it was OK.
ReplyDeleteA second call for it, with a "Cancel" button pressed, returns the first name, no sign the user has pressed "Cancel". Any suggestions ?
I got it! I need to set CancelError to TRUE !
ReplyDeleteThanks
""""To correctly filter the file you should add the '|'. Meaning
ReplyDeleteCommonDialog1.Filter = "Text files (*.txt)|*.txt" instead of
CommonDialog1.Filter = "Text files (*.txt)*.txt""""
Yes this works and corrects showing all files!
Thanks. I was wondering why my filter doesn't work. This solves the problem.
DeleteHow to prompt the user when using the
ReplyDeleteCommonDialog 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?
Hi All. Please how can I get the Visual Basic software? VB6 or any other - full version. Please advise. Thanks in advance.
ReplyDeleteHi, how to filter vsd files?
ReplyDelete