つらつら Excel VBA

私の備忘録です。

フォルダを開く

どれもフォルダを開く処理。エラー処理を忘れないこと。

ThisWorkbook.FollowHyperlink folderPath

CreateObject("WScript.Shell").Run folderPath

shell "explorer " & folderPath, vbNormalFocus

' ファイルを選択した状態でフォルダを開く。フルパスであることに注意。
shell "explorer /select, " & fullPath, vbNormalFocus


フルパスからフォルダパスを取得する1例。

Sub フォルダパスを取得する()
    
    Dim fullPath As String
    fullPath = ThisWorkbook.FullName 'テスト用
    
    Dim FSO As Object
    Set FSO = CreateObject("Scripting.FileSystemObject")
    
    Dim folderPath As String
    folderPath = FSO.getParentFolderName(fullPath)
    
    'shell "explorer " & folderPath, vbNormalFocus ' フォルダを開く
    
End Sub


フォルダ有無の確認用。

If Dir(folderPath, vbDirectory) = "" Then
    ' フォルダが存在しない
Else
    ' OK
End If


If fso.FolderExists(folderPath) = True Then
    'OK
Else
    'フォルダが存在しない
End If


以上。