VB弹出打开对话框,选择文件

2024-12-15 09:52:19
推荐回答(2个)
回答1:

'添加一个CommonDialog控件
'通用声明
Private Declare Function ShellExecute Lib "Shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Const SW_SHOW = 5

'按钮事件
Private Sub Command1_Click()
On Error GoTo userCanceled
With CommonDialog1
.CancelError = True
.Flags = cdlOFNHideReadOnly Or cdlOFNAllowMultiselect Or cdlOFNExplorer Or cdlOFNNoDereferenceLinks
.Filter = "excel文件(*.xls)|*.xls"
.ShowOpen
ShellExecute Me.hwnd, "open", .FileName, "", "", SW_SHOW
End With
userCanceled:
End Sub

回答2:

楼上的是对的,不过可以直接添加一个commondialog控件,这样代码只有两句,这问题怎么问两遍?