gpt4 book ai didi

VBA - 文件夹选择器 - 设置从哪里开始

转载 作者:行者123 更新时间:2023-12-02 14:50:02 25 4
gpt4 key购买 nike

我有一个小型 Access VBA 应用程序,需要用户选择一个文件夹。我想知道是否有办法告诉 VBA 启动文件夹选择器的路径。即从 C:\data\forms 启动文件夹选择器。目前看来是从以前使用的目录开始的。还有一种方法可以限制文件夹选择器可以 Access 的内容。因此它可以 Access C:\data 内的任何内容但 C: 中没有其他任何内容

最佳答案

我多年来一直成功使用以下代码(不是我的代码)。

enter image description here

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

关于VBA - 文件夹选择器 - 设置从哪里开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19372319/

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