gpt4 book ai didi

vba - ms access 浏览文件并获取文件名和路径

转载 作者:行者123 更新时间:2023-12-04 16:53:34 24 4
gpt4 key购买 nike

我正在使用 ms access,我想添加一个按钮来浏览文件,获取文件名及其路径。然后我想将文件路径和文件名存储在 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

最佳答案

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

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

关于vba - ms access 浏览文件并获取文件名和路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14915179/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com