本帖最后由 zhiyeqiangqian 于 2010-03-10 11:14:05 编辑

解决方案 »

  1.   

    p = Left(CommonDialog1.FileName,  InStrRev(CommonDialog1.FileName, "\") ) 
    or
    p = Left(CommonDialog1.FileName, Len(CommonDialog1.FileName) - InStr(Strreverse(CommonDialog1.FileName), "\") ) 
      

  2.   

    Private Sub Command1_Click()
        ' 设置“CancelError”为 True
        CommonDialog1.CancelError = True
        On Error GoTo ErrHandler
        ' 设置标志
        CommonDialog1.Flags = cdlOFNHideReadOnly
        ' 设置过滤器
        CommonDialog1.Filter = "All Files (*.*)|*.*|Text Files" & _
        "(*.txt)|*.txt|Batch Files (*.bat)|*.bat"
        ' 指定缺省的过滤器
        CommonDialog1.FilterIndex = 2
        ' 显示“打开”对话框
        CommonDialog1.ShowOpen
        ' 显示选定文件的名字
        Dim a As Long
        a = InStrRev(CommonDialog1.FileName, "\")
        MsgBox Mid(CommonDialog1.FileName, 1, a - 1)
        Exit Sub
    ErrHandler:
        ' 用户按了“取消”按钮
        Exit Sub
    End Sub
      

  3.   

    楼主误解了InStrRev函数的返回值
    这个函数返回的仍然是从左向右数第几个字符,而不是倒数第几个。
    InStrRev("abcd",c)返回值是3,而不是楼主期望的2.
      

  4.   

    '取得文件全路径函数
    Public Function DeleFilePathName(ByVal Get_FileName As String) As String
           On Error Resume Next
           Dim GetI As Long, GetJ As String, GetK As String
           If Trim(Get_FileName) <> "" Then
              For GetI = Len(Trim(Get_FileName)) To 2 Step -1
                  GetJ = Mid$(Get_FileName, GetI, 1)
                  If GetJ = "\" Then
                     GetK = Left$(Get_FileName, GetI - 1)
                     DeleFilePathName = GetK
                     Exit Function
                  End If
              Next
           End If
    End Function