GEARZ.de

All snippets are without any warranty.
Alle Snippets sind ohne jegliche Gewähr oder Garantie.



TestForFolder:   <back to snippets>

Tests for the existence of a given folder in the same directory where the Excel-workbook is stored in. If the folder doesnt exist, the function tries to create the folder and checks for success. Returns True or False.

Private Function TestForFolder(strPath as String) As Boolean
  strPath = ThisWorkbook.Path & "\" & strPath & "\"
  If Len(Dir(strPath, vbDirectory)) = 0 Then
      If MsgBox("The folder " & Chr(147) & strPath & Chr(148) & " doesnt exist. Do you want the folder to be created?", vbYesNo, "Folder doesnt exist") = vbYes Then
          MkDir (strPath)
          If Len(Dir(strPath, vbDirectory)) = 0 Then
              MsgBox "Error during the folder-creation. Please try to create the folder  " & Chr(147) & strPath & Chr(148) & " manually and try again. " & Chr(13) & "Cant continue, stopping skript", vbCritical, "Folder creation failed"
              TestForImportFolder = False
          Else
              TestForImportFolder = True
          End If
      Else
          MsgBox "Cant continue, stopping skript", vbCritical, "err99"
          TestForImportFolder = False
      End If
  Else
      TestForImportFolder = True
  End If
End Function