所以我写了(也借用了)一些代码来自动导出,下面是我正在使用的文件夹选择器对话框,奇怪的是通过使用 currentproject.path 它从正确的初始目录开始,但说在您导航出去之前该路径不存在然后回到文件夹中。我认为这与选择器在您导航到某个地方或其他地方之前不返回值有关?
这只是一个问题,因为我希望用户能够指定他们想要保存导出的位置,其中包括项目的当前路径。有任何想法吗?
Function GetFolder(Optional startFolder As Variant = -1) As Variant
Dim fldr As FileDialog
Dim vItem As Variant
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = "Select a Folder"
.AllowMultiSelect = False
If startFolder = -1 Then
.InitialFileName = CurrentProject.Path
Else
If Right(startFolder, 1) <> "\" Then
.InitialFileName = startFolder & "\"
Else
.InitialFileName = startFolder
End If
End If
If .Show <> -1 Then GoTo NextCode
vItem = .SelectedItems(1)
End With
NextCode:
GetFolder = vItem
Set fldr = Nothing
Debug.Print GetFolder
End Function
Run Code Online (Sandbox Code Playgroud)
在默认文件夹中添加\,则不会高亮显示。
例如:
.InitialFileName = CurrentProject.Path & "\"
Run Code Online (Sandbox Code Playgroud)
请注意,他们因此可以“点击”确定。