locked
Unable to create folder through VBA RRS feed

  • Question

  • Hi,

    I want to check if a particular folder exist in a given directory. If yes then the code will delete that folder and create a folder with the same name. However if the folder contains any file my code is able to delete the files and folder but unable to create any folder. I'm using the below code:


    
    


    Add a module and paste the code:

    Public Function FileFolderExists(strFullPath As String) As Boolean
    
        On Error GoTo EarlyExit
        If Not Dir(strFullPath, vbDirectory) = vbNullString Then FileFolderExists = True
        
    EarlyExit:
        On Error GoTo 0
    End Function

    Add a command button and add the following code:

    Private Sub CommandButton1_Click()
    If FileFolderExists("C:\Test") Then
            
            Dim FilePath As String
        Dim TestStr As String
    
        FilePath = "C:\Test\*.*"
    
        TestStr = ""
        On Error Resume Next
        TestStr = Dir(FilePath)
        On Error GoTo 0
        If TestStr = "" Then
           
            RmDir "C:\Test\"
        MkDir "C:\Test\"
        Else
            MsgBox "File exist"
        
        Kill "C:\Test\*.*"
        RmDir "C:\Test\"
        MkDir "C:\Test\"
       End If
    Else
           
            MkDir "C:\Test\"
        End If
    End Sub

    I am getting the following error in 

    MkDir "C:\Test\" line

    Path/File Access Error.

    Thanks!!

    Tuesday, May 5, 2015 6:22 PM

Answers

  • The following will do the job:

    Option Explicit
    
    Private Sub CommandButton1_Click()
    Const strFolder As String = "C:\Test"
    Dim fso As Object
        Set fso = CreateObject("Scripting.FileSystemObject")
        If FolderExists(strFolder) Then
            fso.DeleteFolder strFolder
        End If
        CreateFolders strFolder
    lbl_Exit:
        Exit Sub
    End Sub
    
    Private Function FolderExists(strFolderName As String) As Boolean
    Dim fso As Object
        Set fso = CreateObject("Scripting.FileSystemObject")
        If (fso.FolderExists(strFolderName)) Then
            FolderExists = True
        Else
            FolderExists = False
        End If
    lbl_Exit:
        Exit Function
    End Function
    
    Private Function CreateFolders(strPath As String)
    Dim strTempPath As String
    Dim lngPath As Long
    Dim vPath As Variant
        vPath = Split(strPath, "\")
        strPath = vPath(0) & "\"
        For lngPath = 1 To UBound(vPath)
            strPath = strPath & vPath(lngPath) & "\"
            If Not FolderExists(strPath) Then MkDir strPath
        Next lngPath
    lbl_Exit:
        Exit Function
    End Function
    
    


    Graham Mayor - Word MVP
    www.gmayor.com

    • Marked as answer by Deb_chatt Wednesday, May 6, 2015 8:32 AM
    Wednesday, May 6, 2015 6:51 AM

All replies

  • Hi Deb_chatt,

    According to your description, this issue is more related to VBA, the VBA forum is a better place for VBA issue, we will move it there for you.

    Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, May 6, 2015 1:46 AM
  • The following will do the job:

    Option Explicit
    
    Private Sub CommandButton1_Click()
    Const strFolder As String = "C:\Test"
    Dim fso As Object
        Set fso = CreateObject("Scripting.FileSystemObject")
        If FolderExists(strFolder) Then
            fso.DeleteFolder strFolder
        End If
        CreateFolders strFolder
    lbl_Exit:
        Exit Sub
    End Sub
    
    Private Function FolderExists(strFolderName As String) As Boolean
    Dim fso As Object
        Set fso = CreateObject("Scripting.FileSystemObject")
        If (fso.FolderExists(strFolderName)) Then
            FolderExists = True
        Else
            FolderExists = False
        End If
    lbl_Exit:
        Exit Function
    End Function
    
    Private Function CreateFolders(strPath As String)
    Dim strTempPath As String
    Dim lngPath As Long
    Dim vPath As Variant
        vPath = Split(strPath, "\")
        strPath = vPath(0) & "\"
        For lngPath = 1 To UBound(vPath)
            strPath = strPath & vPath(lngPath) & "\"
            If Not FolderExists(strPath) Then MkDir strPath
        Next lngPath
    lbl_Exit:
        Exit Function
    End Function
    
    


    Graham Mayor - Word MVP
    www.gmayor.com

    • Marked as answer by Deb_chatt Wednesday, May 6, 2015 8:32 AM
    Wednesday, May 6, 2015 6:51 AM