tft每日頭條

 > 科技

 > vba怎麼獲取當前文件夾路徑

vba怎麼獲取當前文件夾路徑

科技 更新时间:2025-02-05 10:14:49

vba怎麼獲取當前文件夾路徑?Sub getPath()Range("A1") = "路徑名稱",接下來我們就來聊聊關于vba怎麼獲取當前文件夾路徑?以下内容大家不妨參考一二希望能幫到您!

vba怎麼獲取當前文件夾路徑(獲取常用的路徑并按照選擇的路徑打開指定的文件夾)1

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") = "Application.path"

Range("B7") = Application.Path

Range("A8") = "Application.StartupPath"

Range("B8") = Application.StartupPath

Range("A9") = "Application.TemplatesPath"

Range("B9") = Application.TemplatesPath

Range("A10") = "Application.UserLibraryPath"

Range("B10") = Application.UserLibraryPath

Range("A11") = "Application.DefaultFilePath"

Range("B11") = Application.DefaultFilePath

路徑變量具體路徑SystemDriveC:TEMPC:\Users\WWUHNW~1\AppData\Local\TempwindirC:\WindowsSystemRootC:\WindowsProgramFilesC:\Program FilesApplication.pathC:\Program Files\Microsoft Office\Office12Application.StartupPathC:\Users\username\AppData\Roaming\Microsoft\Excel\XLSTARTApplication.TemplatesPathC:\Users\username\AppData\Roaming\Microsoft\Templates\Application.UserLibraryPathC:\Users\username\AppData\Roaming\Microsoft\AddIns\Application.DefaultFilePathC:\Users\username\Documents2 按照選擇的路徑打開指定的文件夾

選中地址所在單元格,運行下述代碼即可打開指定的文件夾。

Private Declare Function ShellExecute Lib "shell32.dll" 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

,

更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!

查看全部

相关科技资讯推荐

热门科技资讯推荐

网友关注

Copyright 2023-2025 - www.tftnews.com All Rights Reserved