2014-04-14 42 views
3

我需要知道下載文件夾路徑一些Excel的VBA代碼。我怎麼能這樣做?獲取Windows下載文件夾的路徑

既然你可以圍繞下載移動文件夾(也文件而且大多數文件夾,通過文件夾屬性),如%USERPROFILE%環境變量是沒用的構造像%USERPROFILE%\Downloads的路徑,WScript.Shell.SpecialFolders沒有按」列出Downloads文件夾。

我猜它必須讀完註冊表,但我對此毫無頭緒。

謝謝!

+0

' 「C:\用戶\」 &ENVIRON( 「用戶名」)& 「\下載」'? –

+1

整個問題的要點是Downloads文件夾,就像Documents文件夾和許多其他文件夾一樣,可以放在任何地方。雖然User Profile將始終位於'c:\ Users \ simoco'中,但Documents文件夾可以輕鬆移動到'd:\ stuff'。 –

+0

@DmitryPavliv或更簡單:'Environ(「USERPROFILE」)&「\ Downloads」 - 但這並不回答用戶可能會重命名他的Downloads文件夾的問題。 – assylias

回答

4

找到了答案谷歌多一點...

讀取註冊表,按照http://vba-corner.livejournal.com/3054.html方式:

'reads the value for the registry key i_RegKey 
'if the key cannot be found, the return value is "" 
Function RegKeyRead(i_RegKey As String) As String 
Dim myWS As Object 

    On Error Resume Next 
    'access Windows scripting 
    Set myWS = CreateObject("WScript.Shell") 
    'read key from registry 
    RegKeyRead = myWS.RegRead(i_RegKey) 
End Function 

和GUID的Downloads文件夾,按MSDN的http://msdn.microsoft.com/en-us/library/windows/desktop/dd378457(v=vs.85).aspx

{374DE290-123F-4565-9164-39C4925E467B}

RegKeyRead("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\{374DE290-123F-4565-9164-39C4925E467B}")因此產生當前用戶的下載文件夾路徑。

3

支持的方式來閱讀這些路徑是使用SHGetKnownFolderPath功能。

我寫了這個VBA代碼做到這一點。它已在Excel 2000中

它不會工作經過測試的Office中的任何64位版本。我不知道它的Unicode shenanigans是否可以在比2000年更新的Office版本中工作。這不太好。

Option Explicit 

Private Type GuidType 
    data1 As Long 
    data2 As Long 
    data3 As Long 
    data4 As Long 
End Type 

Declare Function SHGetKnownFolderPath Lib "shell32.dll" (ByRef guid As GuidType, ByVal flags As Long, ByVal token As Long, ByRef hPath As Long) As Long 
Declare Function lstrlenW Lib "kernel32.dll" (ByVal hString As Long) As Long 
Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMemory As Long) 
Declare Sub RtlMoveMemory Lib "ntdll.dll" (ByVal dest As String, ByVal source As Long, ByVal count As Long) 

'Read the location of the user's "Downloads" folder 
Function DownloadsFolder() As String 

' {374DE290-123F-4565-9164-39C4925E467B} 
Dim FOLDERID_Downloads As GuidType 
    FOLDERID_Downloads.data1 = &H374DE290 
    FOLDERID_Downloads.data2 = &H4565123F 
    FOLDERID_Downloads.data3 = &HC4396491 
    FOLDERID_Downloads.data4 = &H7B465E92 
Dim result As Long 
Dim hPath As Long 
Dim converted As String 
Dim length As Long 
    'A buffer for the string 
    converted = String$(260, "*") 
    'Convert it to UNICODE 
    converted = StrConv(converted, vbUnicode) 
    'Get the path 
    result = SHGetKnownFolderPath(FOLDERID_Downloads, 0, 0, hPath) 
    If result = 0 Then 
     'Get its length 
     length = lstrlenW(hPath) 
     'Copy the allocated string over the VB string 
     RtlMoveMemory converted, hPath, (length + 1) * 2 
     'Truncate it 
     converted = Mid$(converted, 1, length * 2) 
     'Convert it to ANSI 
     converted = StrConv(converted, vbFromUnicode) 
     'Free the memory 
     CoTaskMemFree hPath 
     'Return the value 
     DownloadsFolder = converted 
    Else 
     Error 1 
    End If 
End Function 
0
Sub GetDownloadedFolderFiles() 
' 
' Keep it simple - Paul Seré 
' 
Dim fso As New FileSystemObject 
Dim flds As Folders 
Dim fls As Files 
Dim f As File 

'Downloads folder for the actual user! 

Set fls = fso.GetFolder("C:\Users\User\Downloads").Files 

For Each f In fls 
    Debug.Print f.Name 
Next 

End Sub 
+0

雖然此代碼段可能會解決問題,但[包括解釋](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)確實有助於提高帖子的質量。請記住,您將來會爲讀者回答問題,而這些人可能不知道您的代碼建議的原因。 –

+0

使用「FileSystemObject」比調用更麻煩的API方法更容易。 GetFolder中的「C:\ Users \ User \ Downloads」參數表示當前用戶的Downloads文件夾。 –

相關問題