VB如何实现一个按钮,点击按钮之后就能够浏览电脑中的文件夹,当选中文件夹之后显示该文件下的所有Excel文

2025-01-04 09:14:05
推荐回答(2个)
回答1:

VB2010的可以吗? “能够浏览电脑中的文件夹”你指在什么 地方什么组件内浏览?”当选中文件夹之后”这里的“选中”是什么操作? 

...............

vb6的:

Private Sub Command1_Click()

    With CommonDialog1

        .DialogTitle = "打开Ecxel文件"

        .Filter = "EXCEL文件|*.xls;*.xlsx"

    .ShowOpen

    

    End With

End Sub

回答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