form1代码(包裹两个command,一个list,一个commondialog)
commonand1为选择文件按钮,另一个是退出
Option ExplicitPrivate Sub Command1_Click()
    
    Dim DlgInfo As DlgFileInfo
    Dim I As Integer
    
    On Error GoTo ErrHandle
    
    '清除List1中的项
    List1.Clear
    
    '选择文件
    With CommonDialog1
        .CancelError = True
        .MaxFileSize = 32767 '被打开的文件名尺寸设置为最大,即32K
        .Flags = cdlOFNHideReadOnly Or cdlOFNAllowMultiselect Or cdlOFNExplorer Or cdlOFNNoDereferenceLinks
        .DialogTitle = "选择文件"
        .Filter = "所有类型的文件(*.*)|*.*"
        .ShowOpen
        DlgInfo = GetDlgSelectFileInfo(.FileName)
        .FileName = ""      '在打开了*.pif文件后须将Filename属性置空,
                            '否则当选取多个*.pif文件后,当前路径会改变
    End With
    
    For I = 1 To DlgInfo.iCount
        List1.AddItem DlgInfo.sPath & DlgInfo.sFile(I)
    Next I
    
    Exit SubErrHandle:
    ' 按了“取消”按钮End SubPrivate Sub Command2_Click()
    End
End Sub
Private Sub Form_Load()End Sub
标准模块代码
Option ExplicitType DlgFileInfo
    iCount As Long
    sPath As String
    sFile() As String
End Type
Public Function GetDlgSelectFileInfo(strFilename As String) As DlgFileInfo
       
    Dim sPath, tmpStr As String
    Dim sFile() As String
    Dim iCount As Integer
    Dim I As Integer
    
    On Error GoTo ErrHandle
    
    sPath = CurDir()  '获得当前的路径,因为在CommonDialog中改变路径时会改变当前的Path
    tmpStr = Right$(strFilename, Len(strFilename) - Len(sPath)) '将文件名分离出来
    
    If Left$(tmpStr, 1) = Chr$(0) Then
        '选择了多个文件(表现为第一个字符为空格)
        For I = 1 To Len(tmpStr)
            If Mid$(tmpStr, I, 1) = Chr$(0) Then
                iCount = iCount + 1
                ReDim Preserve sFile(iCount)
            Else
                sFile(iCount) = sFile(iCount) & Mid$(tmpStr, I, 1)
            End If
        Next I
    Else
        '只选择了一个文件(注意:根目录下的文件名除去路径后没有"\")
        iCount = 1
        ReDim Preserve sFile(iCount)
        If Left$(tmpStr, 1) = "\" Then tmpStr = Right$(tmpStr, Len(tmpStr) - 1)
        sFile(iCount) = tmpStr
    End If
    
    GetDlgSelectFileInfo.iCount = iCount
    ReDim GetDlgSelectFileInfo.sFile(iCount)
    
    If Right$(sPath, 1) <> "\" Then sPath = sPath & "\"
    GetDlgSelectFileInfo.sPath = sPath
    
    For I = 1 To iCount
        GetDlgSelectFileInfo.sFile(I) = sFile(I)
    Next I
    
    Exit FunctionErrHandle:
    MsgBox "GetDlgSelectFileInfo函数执行错误!", vbOKOnly + vbCritical, "自定义函数错误"End Function

解决方案 »

  1.   


      Private Sub Command1_Click() 
      Dim I As Integer 
      Dim Y As Integer 
      Dim Z As Integer 
      Dim FileNames$() 
      Const OFN_ALLOWMULTISELECT = &H200& 
      CommonDialog1.filename = "" 
      CommonDialog1.Filter = "All Files|*.*" 
      CommonDialog1.Flags = OFN_ALLOWMUL- SELECT 
      CommonDialog1.Action = 1 
      CommonDialog1.filename = CommonDialog1. filename & Chr(3 2) 
      Z = 1 
      For I = 1 To Len(CommonDialog1.filename) 
      I = InStr(Z, CommonDialog1.filename, Chr(32)) 
      If I = 0 Then Exit For 
      ReDim Preserve FileNames(Y) 
      FileNames(Y) = Mid(CommonDialog1.filename, Z, I - Z) 
      Z = I + 1 
      Y = Y + 1 
      Next 
      If Y = 1 Then 
      Text1.Text = FileNames(0) 
      Else 
      Text 2.Text = "" 
      For I = 0 To Y - 1 
      If I = 0 Then 
      Text1.Text = FileNames(I) 
      Else 
      Text2.Text = Text2.Text & UCase(FileNames(I)) & Chr$(13) & Chr$(10) 
      End If 
      Next 
      End If 
      End Sub
      

  2.   

    一句话,关键就是 下面这个属性:
    CommonDialog1.Flags = cdlOFNAllowMultiselect