Fin*_*inn 12 ms-access vba access-vba
我有一个小型Access VBA应用程序,要求用户选择一个文件夹.我想知道是否有办法告诉VBA启动文件夹选择器的路径.即启动文件夹选择器C:\data\forms.目前它似乎是从以前使用的目录开始.还有一种方法可以限制文件夹选择器可以访问的内容.所以它可以访问任何内容,C:\data但不能访问任何其他内容C:
Sid*_*out 24
多年来我一直在使用以下代码(Not My Code).

Sub Sample()
Dim Ret
'~~> Specify your start folder here
Ret = BrowseForFolder("C:\")
End Sub
Function BrowseForFolder(Optional OpenAt As Variant) As Variant
'Function purpose: To Browser for a user selected folder.
'If the "OpenAt" path is provided, open the browser at that directory
'NOTE: If invalid, it will open at the Desktop level
Dim ShellApp As Object
'Create a file browser window at the default folder
Set ShellApp = CreateObject("Shell.Application"). _
BrowseForFolder(0, "Please choose a folder", 0, OpenAt)
'Set the folder to that selected. (On error in case cancelled)
On Error Resume Next
BrowseForFolder = ShellApp.self.Path
On Error GoTo 0
'Destroy the Shell Application
Set ShellApp = Nothing
'Check for invalid or non-entries and send to the Invalid error
'handler if found
'Valid selections can begin L: (where L is a letter) or
'\\ (as in \\servername\sharename. All others are invalid
Select Case Mid(BrowseForFolder, 2, 1)
Case Is = ":"
If Left(BrowseForFolder, 1) = ":" Then GoTo Invalid
Case Is = "\"
If Not Left(BrowseForFolder, 1) = "\" Then GoTo Invalid
Case Else
GoTo Invalid
End Select
Exit Function
Invalid:
'If it was determined that the selection was invalid, set to False
BrowseForFolder = False
End Function
Run Code Online (Sandbox Code Playgroud)
art*_*dge 14
这是我一直使用的快速而肮脏的方法.下面的函数只会让用户选择他们想要开始的文件夹 - 我认为限制访问给定路径的最简单方法是在GetFolderName下面针对你想要限制的路径进行检查,例如:
If GetFolderName = "C:\" then
MsgBox("This folder is not for you buddy")
Exit Sub
end if
Run Code Online (Sandbox Code Playgroud)
也不是我的代码:)
Public Function GetFolderName(Optional OpenAt As String) As String
Dim lCount As Long
GetFolderName = vbNullString
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = OpenAt
.Show
For lCount = 1 To .SelectedItems.Count
GetFolderName = .SelectedItems(lCount)
Next lCount
End With
End Function
Run Code Online (Sandbox Code Playgroud)
如果您不需要将文件夹视图限制为您的用户,那么我建议您使用FileDialog方法(界面比调用Shell的功能更直观)。有关更多详细信息,您可以在CPearson的网站上阅读更多内容。他有一篇很长的文章,内容涉及使用VBA浏览文件夹的方法(多种方法; FileDialog选项位于最后):
Function BrowseFolder(Title As String, _
Optional InitialFolder As String = vbNullString, _
Optional InitialView As Office.MsoFileDialogView = _
msoFileDialogViewList) As String
Dim V As Variant
Dim InitFolder As String
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = Title
.InitialView = InitialView
If Len(InitialFolder) > 0 Then
If Dir(InitialFolder, vbDirectory) <> vbNullString Then
InitFolder = InitialFolder
If Right(InitFolder, 1) <> "\" Then
InitFolder = InitFolder & "\"
End If
.InitialFileName = InitFolder
End If
End If
.Show
On Error Resume Next
Err.Clear
V = .SelectedItems(1)
If Err.Number <> 0 Then
V = vbNullString
End If
End With
BrowseFolder = CStr(V)
End Function
Run Code Online (Sandbox Code Playgroud)
该函数有两个参数。第一个标题是一个字符串,指定要与文件对话框一起显示的标题。第二个InitialFolder是可选的,它指定对话框应打开的初始文件夹。第三个参数(也是可选的)InitialView指定视图类型。有关此参数的有效值,请参见对象浏览器中的MsoFileDialogView。该函数返回用户选择的标准文件夹名称,如果用户取消对话框,则返回空字符串。
这是一个更简单的方法。此代码片段让用户选择一个文件夹,然后将该文件夹地址打印到屏幕上:
Sub PrintSelectedFolder()
Dim selectedFolder
With Application.FileDialog(msoFileDialogFolderPicker)
.Show
selectedFolder = .SelectedItems(1)
End With
'print to screen the address of folder selected
MsgBox (selectedFolder)
End Sub
Run Code Online (Sandbox Code Playgroud)