B-Teck!

お仕事からゲームまで幅広く

【VBA】与えられたパスのフォルダを新しく作成する

パスを指定してフォルダを作成する。

下記みたいな指定の場合でも存在しないパスごと作ってくれる。

    MakeNotExistsDir("存在するパスA\存在しないパスA")  
    MakeNotExistsDir("存在しないパスA\存在しないパスB\存在しないパスC")  

 

Option Explicit

'WindowsのAPIを使うので環境によって宣言を分ける
#If VBA7 And Win64 Then
    Declare PtrSafe Function SHCreateDirectoryEx Lib "shell32" Alias "SHCreateDirectoryExA" _
               (ByVal hwnd As LongPtr, ByVal pszPath As String, ByVal psa As LongPtr) As Long
#Else
    Declare Function SHCreateDirectoryEx Lib "shell32" Alias "SHCreateDirectoryExA" _
             (ByVal hwnd As Long, ByVal pszPath As String, ByVal psa As Long) As Long
#End If

'/**
' * MakeNotExistsDir
' * 与えられたパスのフォルダを新しく作る
' * @param DirPath              : 作成するフォルダのパス
' */
Sub MakeNotExistsDir(ByVal DirPath As String)
    Call SHCreateDirectoryEx(0&, DirPath, 0&)
End Sub