如何在VB6中使用“打开文件”对话框?

如何在VB6中使用“打开文件”对话框?,vb6,Vb6,我想从目录或其他系统中选择一个文件。如何在VB6中使用“打开文件”对话框?中有一些示例代码。引述: 在VB6中,添加组件: 项目>组件 在“控件”选项卡上,选择Microsoft Common Dialog Control 6.0(SP6) 现在,在窗体上,从工具箱中添加新的公共对话框控件 在代码中,您需要: CommonDialog1.Filter = "Apps (*.txt)|*.txt|All files (*.*)|*.*" CommonDialog1.DefaultExt = "

我想从目录或其他系统中选择一个文件。如何在VB6中使用“打开文件”对话框?

中有一些示例代码。引述:

在VB6中,添加组件:

  • 项目>组件
  • 在“控件”选项卡上,选择Microsoft Common Dialog Control 6.0(SP6)
现在,在窗体上,从工具箱中添加新的公共对话框控件

在代码中,您需要:

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
它需要“1”,但效果很好,谢谢

CommonDialog1.Filter = "Apps (*.txt)|*.txt|All files (*.*)|*.*"
CommonDialog1.DefaultExt = "txt"
CommonDialog1.DialogTitle = "Select File"
CommonDialog1.ShowOpen
'FileName属性为您提供了需要使用的变量
MsgBox CommonDialog1.FileName

我修改了@Ant的答案这看起来很有帮助,但请在答案中添加文字解释一些上下文。特别是因为与问题相比,这是一个迟来的答案,这将有助于了解为什么您的解决方案不同&更好。如果我的理解是正确的,这是针对VBA而不是VB6的。VB6中不存在Application.FileDialog。
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