0

首先,我必须说我在一台安装了很少引用且根本没有 Office 的机器上运行代码。

我需要使用Shell命令打开一个“explorer.exe”实例,浏览文件夹,输入一个选定的(或新创建的)实例,最后单击一个MsgBox(同时弹出)上的“确定”以关闭Shell并返回变量的选定文件夹路径。

我不知道如何实现这一目标。当我使用 时CurDir,我最终得到一个不是所选文件夹的文件夹。

使用的代码(不起作用):

Sub BrowseForFolder()

ActualDir = "D:\"
Call Shell("C:\Windows\explorer", ActualDir, 1)

If Msgbox("Browse into folder or create a new one and then browse into it, then click ok", vbOkOnly, "Browse") = vbOk Then
    ' here should be the command to return the path. The following doesn't work since it returns always "D:\"
    ActualDir = CurDir
End If

' Here I have to close the Shell - I have no idea what to write to do it

End Sub
4

1 回答 1

0

在 LS_dev 建议的代码下方,它完全有效:

Private Sub SelectFolder_Click()

Dim objShell As Shell       

必须添加外壳引用

Dim ssfWINDOWS As Long
Dim objFolder As Folder2    

由于某种原因“文件夹”不起作用

ssfWINDOWS = 36
Set objShell = New Shell
Set objFolder = objShell.BrowseForFolder(0, "Select Folder", 0, "D:\")

If (Not objFolder Is Nothing) Then
    FolderName = objFolder.Self.path
End If

Set objShell = Nothing

End Sub
于 2013-10-10T09:12:24.820 回答