您的位置 首页 > 娱乐休闲

VBA|获取常用的路径并按照选择的路径打开指定的文件夹

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

运行结果:

路径变量具体路径
SystemDriveC:
TEMPC:\Users\WWUHNW~1\AppData\Local\Temp
windirC:\Windows
SystemRootC:\Windows
ProgramFilesC:\Program Files
AC:\Program Files\Microsoft Office\Office12
AC:\Users\username\AppData\Roaming\Microsoft\Excel\XLSTART
AC:\Users\username\AppData\Roaming\Microsoft\Templates\
AC:\Users\username\AppData\Roaming\Microsoft\AddIns\
AC:\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

责任编辑: 鲁达

1.内容基于多重复合算法人工智能语言模型创作,旨在以深度学习研究为目的传播信息知识,内容观点与本网站无关,反馈举报请
2.仅供读者参考,本网站未对该内容进行证实,对其原创性、真实性、完整性、及时性不作任何保证;
3.本站属于非营利性站点无毒无广告,请读者放心使用!

“vb.net如何获取文件上一级路径”边界阅读