I want to select a file from the directory or other system. How to use open file dialog in VB 6?
3 Answers
There's some example code in this question. Quoting:
In VB6, add the component:
- Project > Components
- On the Controls tab, choose Microsoft Common Dialog Control 6.0 (SP6)
Now on your form, add the new Common Dialog control from the toolbox
In code, you need:
CommonDialog1.Filter = "Apps (*.txt)|*.txt|All files (*.*)|*.*"
CommonDialog1.DefaultExt = "txt"
CommonDialog1.DialogTitle = "Select File"
CommonDialog1.ShowOpen
'The FileName property gives you the variable you need to use
MsgBox CommonDialog1.FileName
it needed the "1", but works great thank you
CommonDialog1.Filter = "Apps (*.txt)|*.txt|All files (*.*)|*.*"
CommonDialog1.DefaultExt = "txt"
CommonDialog1.DialogTitle = "Select File"
CommonDialog1.ShowOpen
'The FileName property gives you the variable you need to use MsgBox CommonDialog1.FileName
Sub main()
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xls; *.xlsb", 1
.Show
fullpath = .SelectedItems.Item(1)
End With
If InStr(fullpath, ".xls") = 0 Then
Exit Sub
End If
Workbooks.Open fullpath
End Sub
-
This looks helpful, but please add text to your answer explaining some context. Especially since this is a late answer compared to the question, it will help to know why your solution is different & better. Mar 4, 2020 at 21:06
-
If my understanding is correct this is for VBA and not VB6. Application.FileDialog does not exist in VB6. Mar 26, 2020 at 12:20