vba ms access 浏览文件并获取文件名和路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14915179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ms access browse for file and get file name and path
提问by derek
I am using ms access and i want to add a button to browse for a file, get the name of the file and its path . i then want to store the file path and file name in 2 separate variables. The code i have so far is below and at the moment i can browse for a file and get the name of the file only. Can anyone help me add to my code to get the file path and to store both the file name and file path in separate variables.
我正在使用 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
回答by Fionnuala
You are passing the full path to your function, so you can get the path from that. For example:
您正在将完整路径传递给您的函数,因此您可以从中获取路径。例如:
Public Function Filename(ByVal strPath As String, sPath) As String
sPath = Left(strPath, InStrRev(strPath, "\"))
Filename = Mid(strPath, InStrRev(strPath, "\") + 1)
End Function
Called by, say:
被调用,说:
sFile = Filename(f.SelectedItems(i), sPath)
MsgBox sPath & "---" & sFile
In full
在全
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
回答by HansUp
For what you want from your click event procedure, there is no need to call a separate custom VBA function.
对于您想要的点击事件过程,无需调用单独的自定义 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
回答by Vlado
Another approach what I use to load an Excel file:
我用来加载 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