【问题标题】:ms access browse for file and get file name and pathms access 浏览文件并获取文件名和路径
【发布时间】:2013-02-16 21:14:26
【问题描述】:

我正在使用 ms 访问,我想添加一个按钮来浏览文件,获取文件名及其路径。然后我想将文件路径和文件名存储在 2 个单独的变量中。到目前为止我的代码如下,目前我可以浏览文件并仅获取文件名。谁能帮我添加到我的代码中以获取文件路径并将文件名和文件路径存储在单独的变量中。

Private Sub Command7_Click()

Dim f As Object

Set f = Application.FileDialog(3)

f.AllowMultiSelect = True

If f.Show Then
    For i = 1 To f.SelectedItems.Count
        MsgBox Filename(f.SelectedItems(i))
    Next
End If

End Sub


Public Function Filename(ByVal strPath As String) As String

If Right$(strPath, 1) <> "\" And Len(strPath) > 0 Then
    Filename = Filename(Left$(strPath, Len(strPath) - 1)) + Right$(strPath, 1)

End If

End Function

【问题讨论】:

    标签: vba ms-access


    【解决方案1】:

    您正在将完整路径传递给您的函数,因此您可以从中获取路径。例如:

    Public Function Filename(ByVal strPath As String, sPath) As String
        sPath = Left(strPath, InStrRev(strPath, "\"))
        Filename = Mid(strPath, InStrRev(strPath, "\") + 1)
    End Function
    

    打电话给,说:

        sFile = Filename(f.SelectedItems(i), sPath)
        MsgBox sPath & "---" & sFile
    

    完整

    Private Sub Command7_Click()
    
    Dim f As Object
    
    Set f = Application.FileDialog(3)
    
    f.AllowMultiSelect = True
    
    If f.Show Then
        For i = 1 To f.SelectedItems.Count
            sFile = Filename(f.SelectedItems(i), sPath)
            MsgBox sPath & "---" & sFile
        Next
    End If
    
    End Sub
    
    
    Public Function Filename(ByVal strPath As String, sPath) As String
        sPath = Left(strPath, InStrRev(strPath, "\"))
        Filename = Mid(strPath, InStrRev(strPath, "\") + 1)
    End Function
    

    【讨论】:

    • 嗨 Remou,感谢您的回复。如何将文件名和文件路径存储在两个单独的变量中,我需要使用这些变量将文件复制到新文件夹
    • 看函数会发现路径存储在sPath中,文件名由Filename返回。
    • 我复制了你的代码,我得到“用户定义类型未定义”和“f as Filedialog”突出显示
    • 好吧,我承认,只是将其更改为 object,但该声明不是原始示例的一部分。
    • 你确定这条线是正确的Public Function Filename(ByVal strPath As String, sPath) 吗?您是在使用我发布的代码还是进行了更改?
    【解决方案2】:

    对于您想要的点击事件过程,无需调用单独的自定义 VBA 函数。

    Private Sub Command7_Click()
        Dim f As Object
        Dim strFile As String
        Dim strFolder As String
        Dim varItem As Variant
    
        Set f = Application.FileDialog(3)
        f.AllowMultiSelect = True
        If f.Show Then
            For Each varItem In f.SelectedItems
                strFile = Dir(varItem)
                strFolder = Left(varItem, Len(varItem) - Len(strFile))
                MsgBox "Folder: " & strFolder & vbCrLf & _
                    "File: " & strFile
            Next
        End If
        Set f = Nothing
    End Sub
    

    【讨论】:

      【解决方案3】:

      我用来加载 Excel 文件的另一种方法:

      Public Sub Command7_Click()
          Dim FD As FileDialog
          Dim fileNamePath As String, fileExtension As String, fileName As String
          If fileNamePath = "" Then
              Set FD = Application.FileDialog(msoFileDialogOpen)
              Dim FileChosen As Integer
              FileChosen = FD.show
              FD.Title = "Choose workbook"
              FD.InitialView = msoFileDialogViewList
      
              FD.Filters.Clear
              FD.Filters.Add "Excel workbooks", "*.xlsx"
              FD.Filters.Add "All files", "*.*"
              FD.FilterIndex = 1
              FD.ButtonName = "Choose this file"
              If FileChosen <> -1 Then 'didn't choose anything (clicked on CANCEL)
                  MsgBox "No file opened", vbCritical
              Else
                  fileNamePath = FD.SelectedItems(1)
                  fileName = Dir(fileNamePath)
                  fileExtension = Right$(fileName, Len(fileName) - InStrRev(fileName, "."))
              End If
              Set FD = Nothing
          End If
      End Sub
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-03-13
        • 2019-12-15
        • 2021-11-28
        • 1970-01-01
        • 2013-09-12
        • 1970-01-01
        • 2020-12-31
        • 2017-06-22
        相关资源
        最近更新 更多