Skip to content

Instantly share code, notes, and snippets.

@TSKGunGun
Last active February 6, 2017 09:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TSKGunGun/0519fe9cff507e45b1c7c02519758c12 to your computer and use it in GitHub Desktop.
Save TSKGunGun/0519fe9cff507e45b1c7c02519758c12 to your computer and use it in GitHub Desktop.
再帰的にフォルダを作成 http://www.tetsuyanbo.net/tetsuyanblog/24247から
Public Function CreateFolder(strFolderPath As String)
' パスを\区切りで分ける
Dim varFolders As Variant ' フォルダ名リスト
varFolders = Split(strFolderPath, "\")
' FileSystemObjectをインスタンス化する
Dim objFileSystemObject As Object ' FileSystemObjectオブジェクト
Set objFileSystemObject = CreateObject("Scripting.FileSystemObject")
' フォルダを作成する
Dim varValue As Variant ' フォルダ名リストから取り出したフォルダ名
Dim strValue As String ' CreateFolder()の戻り値
For Each varValue In varFolders
' 上位層からのフォルダパスを作る
strCurrentPath = strCurrentPath & varValue & "\"
' フォルダがあるかチェックする
On Error Resume Next
If objFileSystemObject.FolderExists(strCurrentPath) = False Then
' フォルダがなかったら新しく作る
strValue = objFileSystemObject.CreateFolder(strCurrentPath)
End If
' エラーをチェックする
If Err <> 0 Then
' フォルダの作成に失敗したらFor文を終了する
MsgBox "フォルダの作成に失敗しました"
Exit For
End If
Next
' 使い終わったら必ずNothingを設定する
Set objFileSystemObject = Nothing
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment