none
Create Folder/Directory Assistance RRS feed

  • Question

  • Hello - I have borrowed some code from this site and seem to be running into an error (highlighted below) when running...can anyone see a problem with the below...or maybe even a better/easier way to create a folder?

    Option Explicit

    Private Sub Create_Folder_Click()

        Const strFolder As String = "\\NCAPP\users\fOSUserName()\My Documents\NewUserTemps"

        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

    ----------------------------------------------------------------------

    Error received (for highlighted section)


    Wednesday, February 17, 2016 10:59 PM

Answers

  • (1) The function CreateFolders wasn't intended to handle UNC paths (paths beginning with \\). Change it like this:

    Function CreateFolders(strPath As String)
        Dim strTempPath As String
        Dim lngPath As Long
        Dim vPath As Variant
        Dim f As Boolean
        If Left(strPath, 2) = "\\" Then
            strPath = Mid(strPath, 3)
            f = True
        End If
        vPath = Split(strPath, "\")
        strPath = vPath(0) & "\"
        If f Then
            strPath = "\\" & strPath
        End If
        For lngPath = 1 To UBound(vPath)
            strPath = strPath & vPath(lngPath) & "\"
            If Not FolderExists(strPath) Then
                MkDir strPath
            End If
        Next lngPath
    End Function


    (2) Is fOSUserName() a function that returns the name of the logged in user? If so, you cannot use it inside a string. Change

        Const strFolder As String = "\\NCAPP\users\fOSUserName()\My Documents\NewUserTemps"

    to

        Dim strFolder As String
        strFolder = "\\NCAPP\users\" & fOSUserName & "\My Documents\NewUserTemps"

    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Thursday, February 18, 2016 5:50 PM

All replies

  • Is the code for the FolderExists function in the same module as Create_Folder_Click? If not, change

    Private Function FolderExists(strFolderName As String) As Boolean

    to

    Public Function FolderExists(strFolderName As String) As Boolean

    and similar for

    Private Function CreateFolders(strPath As String)


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Wednesday, February 17, 2016 11:33 PM
  • Hi Hans and thank you for your response :)

    I did change to a Public Function as you recommended. Lol...that got me a little farther along in the processing of the code, but ran into another error indicated below :(

    Any ideas about this one?

    Thursday, February 18, 2016 5:08 PM
  • Oh and also...I get the same result whether a Public Function or Private Function within the same module as the Create_Folder_Click?
    Thursday, February 18, 2016 5:10 PM
  • (1) The function CreateFolders wasn't intended to handle UNC paths (paths beginning with \\). Change it like this:

    Function CreateFolders(strPath As String)
        Dim strTempPath As String
        Dim lngPath As Long
        Dim vPath As Variant
        Dim f As Boolean
        If Left(strPath, 2) = "\\" Then
            strPath = Mid(strPath, 3)
            f = True
        End If
        vPath = Split(strPath, "\")
        strPath = vPath(0) & "\"
        If f Then
            strPath = "\\" & strPath
        End If
        For lngPath = 1 To UBound(vPath)
            strPath = strPath & vPath(lngPath) & "\"
            If Not FolderExists(strPath) Then
                MkDir strPath
            End If
        Next lngPath
    End Function


    (2) Is fOSUserName() a function that returns the name of the logged in user? If so, you cannot use it inside a string. Change

        Const strFolder As String = "\\NCAPP\users\fOSUserName()\My Documents\NewUserTemps"

    to

        Dim strFolder As String
        strFolder = "\\NCAPP\users\" & fOSUserName & "\My Documents\NewUserTemps"

    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Thursday, February 18, 2016 5:50 PM
  • Hans...you are da man!Thank you soooo much for your assistance! Worked like a charm! Lol...once I placed my FsoUsername function into this workbook of course...worked much better with that in place ;-)

    Thank you again Hans! You are AWESOME!!!!

    Thursday, February 18, 2016 6:08 PM