1 获取常用的路径
Sub getPath()
Range("A1") = "路径名称"
Range("B1") = "具体路径"
Range("A2") = "SystemDrive"
Range("B2") = Environ("SystemDrive")
Range("A3") = "TEMP"
Range("B3") = Environ("TEMP")
Range("A4") = "windir"
Range("B4") = Environ("windir")
Range("A5") = "SystemRoot"
Range("B5") = Environ("SystemRoot")
Range("A6") = "ProgramFiles"
Range("B6") = Environ("ProgramFiles")
Range("A7") = "A;
Range("B7") = A
Range("A8") = "A;
Range("B8") = A
Range("A9") = "A;
Range("B9") = A
Range("A10") = "A;
Range("B10") = A
Range("A11") = "A;
Range("B11") = A
Range("A12") = "A;
Range("B12") = A
End Sub
运行结果:
路径变量 | 具体路径 |
SystemDrive | C: |
TEMP | C:\Users\WWUHNW~1\AppData\Local\Temp |
windir | C:\Windows |
SystemRoot | C:\Windows |
ProgramFiles | C:\Program Files |
A | C:\Program Files\Microsoft Office\Office12 |
A | C:\Users\username\AppData\Roaming\Microsoft\Excel\XLSTART |
A | C:\Users\username\AppData\Roaming\Microsoft\Templates\ |
A | C:\Users\username\AppData\Roaming\Microsoft\AddIns\ |
A | C:\Users\username\Documents |
2 按照选择的路径打开指定的文件夹
选中地址所在单元格,运行下述代码即可打开指定的文件夹。
Private Declare Function ShellExecute Lib "; Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal sParam As Long, ByVal lParam As Long) As Long
Private Const WM_CLOSE As Long = &H10
'以上代码声明API函数和常量
Public Sub OpenDirectory()
Dim path1 As String
path1 = ActiveCell
ShellExecute 0, "", "", "", path1, 1
End Sub
Public Sub CloseDirectory()
Dim hWnd As Long
hWnd = FindWindows(vbNullString, "C:\Program Files\Microsoft Office\Office12")
PostMessage hWnd, WM_CLOSE, 0, 0
End Sub