Results 1 to 4 of 4

Thread: Create a Dir

  1. #1
    Lounger
    Join Date
    May 2012
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Create a Dir

    Hi working on a file that creates its owe file and create a dir to save the file in, I am getting an error and wondered it anyone can help, this is my code:
    The error occurs in the Public Function CreateDir(ByVal strDir As String) at the Select Case Left(strDir, 2) line


    Option Explicit

    Public Function AddSlash(FolderName As String) As String

    If FolderName = "" Then
    AddSlash = ""
    Exit Function
    End If

    If Right(FolderName, 1) <> "\" Then
    AddSlash = FolderName & "\"
    Else
    AddSlash = FolderName
    End If

    End Function


    Public Function CreateDir(ByVal strDir As String)

    Dim iLoc As Integer

    If Dir(strDir, vbDirectory) <> "" Then
    Exit Function
    End If

    strDir = FileOrFolderName(strDir, False)
    strDir = AddSlash(strDir)

    Select Case Left(strDir, 2)
    Case "\"
    iLoc = InStr(3, strDir, "\")
    iLoc = InStr(iLoc + 1, strDir, "\")
    Case Else
    iLoc = InStr(strDir, "\")
    End Select


    Do Until iLoc = 0
    If Dir(Left(strDir, iLoc), vbDirectory) = "" Then
    MkDir Left(strDir, iLoc)
    End If
    iLoc = InStr(iLoc + 1, strDir, "\")
    Loop

    End Function



    Public Function FileOrFolderName(InputString As String, ReturnFileName As Boolean) As String

    Dim i As Integer, FolderName As String, Filename As String

    i = 0

    While InStr(i + 1, InputString, "\") > 0
    i = InStr(i + 1, InputString, "\")
    Wend

    If i = 0 Then
    FolderName = CurDir
    Else
    FolderName = Left(InputString, i - 1)
    End If

    Filename = Right(InputString, Len(InputString) - i)

    If ReturnFileName Then
    FileOrFolderName = Filename
    Else
    FileOrFolderName = FolderName
    End If



    End Function

    Public Sub CloseObject(obj)
    On Error Resume Next
    Static i As Long

    If Not obj Is Nothing Then
    Debug.Print i & vbTab & Now() & vbTab & obj.Fields(0).Name & vbTab & obj.Fields.Count
    i = i + 1

    obj.Close
    Set obj = Nothing



    End If


    End Sub

  2. #2
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,434
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    Hello,

    I ran your code from the Immediate Window in Excel 2003 with this command ?CreateDir("G:\Test") and it did not generate an error. I set a break point right after the Select statement at which point iLoc = 3 and strDir = G:\
    Single stepping through the code from there caused no errors either but of course it did not create the Directory "Test" because your code thought it was the file name.

    Can you tell us what drive:\path\fn.ext you are passing to CreateDir?

    FYI: when posting code enclose it in [code] [/code] tags.
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  3. #3
    Lounger
    Join Date
    May 2012
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts
    the strDir is \\mss001coffc03\usersbb\mounsey\cliental\YTD2012\F red\Reports\Test\Draft\DPReports\109203_Q1\

  4. #4
    WS Lounge VIP
    Join Date
    Mar 2002
    Location
    Newcazzle, UK
    Posts
    2,826
    Thanks
    136
    Thanked 482 Times in 459 Posts
    Hi

    As you seem to be using a network folder path, have the appropriate network permissions been set to allow the particular User to create folders?

    zeddy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •