VB如何实现一个按钮,点击按钮之后就能够浏览电脑中的文件夹,当选中文件夹之后显示该文件下的所有Excel文
2个回答
展开全部
Private Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
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 OFN_HIDEREADONLY = &H4
Const OFN_PATHMUSTEXIST = &H800
Const OFN_FILEMUSTEXIST = &H1000
Private Sub Command1_Click()
Dim ofn As OPENFILENAME
Dim rtn As String
Dim sFile As String
ofn.lStructSize = Len(ofn)
ofn.hwndOwner = Me.hwnd
ofn.flags = OFN_HIDEREADONLY Or OFN_PATHMUSTEXIST Or OFN_FILEMUSTEXIST
ofn.lpstrFile = String(255, 0)
ofn.nMaxFile = 255
ofn.lpstrFileTitle = String(255, 0)
ofn.nMaxFileTitle = 255
ofn.lpstrInitialDir = App.Path & "\"
ofn.lpstrFilter = "excel工作簿(*.xls)" & Chr$(0) & "*.xls;" & Chr$(0) & Chr$(0) '打开的文件类型
ofn.nFilterIndex = 1
ofn.lpstrTitle = "打开文件"
rtn = GetOpenFileName(ofn)
If rtn > 0 Then
sFile = Left(ofn.lpstrFile, InStr(ofn.lpstrFile, Chr(0)) - 1)
ShellExecute Me.hwnd, "open", sFile, vbNullString, vbNullString, 1
End If
End Sub
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
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 OFN_HIDEREADONLY = &H4
Const OFN_PATHMUSTEXIST = &H800
Const OFN_FILEMUSTEXIST = &H1000
Private Sub Command1_Click()
Dim ofn As OPENFILENAME
Dim rtn As String
Dim sFile As String
ofn.lStructSize = Len(ofn)
ofn.hwndOwner = Me.hwnd
ofn.flags = OFN_HIDEREADONLY Or OFN_PATHMUSTEXIST Or OFN_FILEMUSTEXIST
ofn.lpstrFile = String(255, 0)
ofn.nMaxFile = 255
ofn.lpstrFileTitle = String(255, 0)
ofn.nMaxFileTitle = 255
ofn.lpstrInitialDir = App.Path & "\"
ofn.lpstrFilter = "excel工作簿(*.xls)" & Chr$(0) & "*.xls;" & Chr$(0) & Chr$(0) '打开的文件类型
ofn.nFilterIndex = 1
ofn.lpstrTitle = "打开文件"
rtn = GetOpenFileName(ofn)
If rtn > 0 Then
sFile = Left(ofn.lpstrFile, InStr(ofn.lpstrFile, Chr(0)) - 1)
ShellExecute Me.hwnd, "open", sFile, vbNullString, vbNullString, 1
End If
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询