2013-10-10 127 views
0

首先,我必須說,我在一臺安裝了很少引用且根本沒有Office的計算機上運行代碼。VBA - 返回Shell explorer當前路徑,然後關閉它

我需要用Shell命令打開一個「explorer.exe」實例,瀏覽文件夾,輸入一個選定的(或新創建的),最後點擊MsgBox(同時彈出)上的「Ok」關閉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 
+1

這是錯誤的方法:改爲檢查[Shell.BrowseForFolder](http://msdn.microsoft.com/en-us/library/windows/desktop/bb774065(v = vs.85).aspx)。你在使用[tag:VBA]還是[tag:VBScript]? –

+0

我正在使用vba。我現在就給它一個鏡頭,讓你知道它是否有效 – Noldor130884

+0

沒有Office,那麼VBA在哪個環境下?你可以從'應用程序'有更好的設施。 –

回答

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 
相關問題