如何从“打开”对话框中 获取用户所打开文件的路径的字符串?

解决方案 »

  1.   

    dim str as string
    commonDialog1.showSave
    str=commonDialog1.filename
      

  2.   

    StrFileName=dialog1.filename
    StrFilePath=Left(StrFileName,Instrrev(strFileName,"\"))
      

  3.   

    修改一下:dim str as string
    commonDialog1.showopen
    str=commonDialog1.filenamestr即为路径及文件名称
      

  4.   

    Option ExplicitPrivate Sub Command1_Click()
        On Error GoTo myerr
        Me.CommonDialog1.CancelError = True
        Me.CommonDialog1.Filter = "all files(*.*)|*.*"
        Me.CommonDialog1.ShowOpen
        Dim filename As String
        filename = Me.CommonDialog1.filename
        If filename = "" Then Exit Sub
        '下面写你的语句
        Exit Sub
    myerr:
        Select Case Err.Number
        Case 32755
        MsgBox "你选择了取消"
        Exit Sub
        End Select
        
    End Sub
      

  5.   

    在窗体上拖放CommonDialog控件,然后写代码。Private Sub Command1_Click()On Error GoTo Nofile
      
      CommonDialog1.Filter = "Text (*.txt)|*.txt|Pictures (*.bmp;*.ico)|*.bmp;*.ico"
      
      CommonDialog1.FilterIndex = 2
      
      CommonDialog1.DialogTitle = "打开文件"
      
      CommonDialog1.FileName = ""
      
      CommonDialog1.Flags = cdlOFNAllowMultiselect Or cdlOFNHideReadOnly
      
      ' CommonDialog1.Action = 1
      
      CommonDialog1.ShowOpen
      
      PathFile = CommonDialog1.FileName
      
      MsgBox PathFile
      
      Exit Sub
      
    Nofile:
           
        If Err.Number <> 32755 Then
        
           MsgBox "打开文件出现未知错误!"
           
        End If
      
    End Sub
    当然,如果不想由程序来控件,可以在控件属性Open/Save项设置,然后按楼上的写代码就行了。