Second Error does not get trapped

DKDiveDude

Registered User.
Local time
Today, 09:31
Joined
Mar 28, 2003
Messages
56
I am trying to make some code that will automatically create folders that do no exist, when saving a file.

I have something like this (simplified):

Function SaveFile(strFileName As String, strData As String)
On Error GoTo CreateFolder

Open strFileName for Output As #1
Print #1, strData
Close #1

Exit Function

CreateFolder:
Here is code to cut of filename, test and create folder one at a time
..
..
MkDir StrFolderTest
Resume Next

End Function

The problem is that if the path of strFolderTest does not exist, MkDir return an error that I can't trap, and the program exits.

Is that because I already trapped one error, I mean if I just do a simple function that will make a folder, I can trap it fine like this:

Function MakeFolder(strFolder As String)
On Error GoTo ErrorHandler
MkDir StrFolder
Exit Function

ErrorHandler:
Jadi, jadi, jadi
Resume Next
End Function

Obviously I am doing something wrong here, but what is the million dollar quesition.

Thanks
 
You need to use the Dir() function to test if the directory already exists. If it does not then you need to create it.
Code:
    If Dir("C:\Test\DirX", vbDirectory) = "" Then
        MkDir ("C:\Test\DirX")
    End If
HTH
 
Thanks HTH, that's good.

But what if my path is this:

"C\Data\Stuff\Documents\Word\Forms"

AND

C:\Data does NOT even exist?

In that case your example would test and go ahead and try to create the folder, and then an error occurs, because the previous folder do not exist.

I really want to dig backwards a folder at a time, to test if it's there, and then create ALL folders that are missing in the path.

If it where not because VBA is in a special state, and can't trap another error when it's already trapping, my code would be working fine.

Thanks anyways...
 
DK,

Interesting problem. This works, I haven't had too much
time to play lately, so this was fun.

Take great care if you modify this code! If you modify the
ELSE part of the code and put a call to CheckDir it will
wallow about until it runs out of resources (stack space).

It is kind of neat to watch it run with the debugger tho ...

An alternative method is to iteratively just build the
Dir string from left-to-right and put the same code in
a loop. But that wouldn't have been any fun at all.

Code:
Call by:  CheckDir ("c:\zzz\1\2\3")

Doesn't return anything.  Don't care to.

Public Function CheckDir(strPath As String)
' +-----------------------------------------------+
' | If a nested directory Then                    |
' |    process the higher levels: recursive       |
' |    process this directory                     |
' | Else                                          |
' |    process terminal directory: non-recursive  |
' | End If                                        |
' +-----------------------------------------------+
If InStrRev(strPath, "\") > 3 Then
   CheckDir (Mid(strPath, 1, InStrRev(strPath, "\") - 1))
   If Dir(strPath, vbDirectory) = "" Then
      MkDir (strPath)
   End If
Else
   If Dir(strPath, vbDirectory) = "" Then
      MkDir (strPath)
   End If
End If
End Function

Thanks to ghudson. I don't play with file systems too
much and he's had some good examples posted here.

Wayne
 
Wayne,

thanks for your recursive example, more neater than the solution I finally came up with, with GOTO's that reminded me of the bad old BASIC days :)

DKDiveDude

Silly Quote of the Day:

"Scuba Divers works best under pressure!"
 
Thanks for posting your solution Wayne. I can not use it [yet] for I am still using Office 97 and the InStrRev function was not invented untill Office 2000. I was in need of it today but luckily I found the code below that homer2002 posted. hoomer2002 stated that it does not work for UNC paths but it is working for me and that is all that really matters.
;)
Code:
Private Sub Test()
    CreateDirectory ("\\Server\Partition\Directory\Sub1\Sub2")
End Sub
    
Public Sub CreateDirectory(sPath As String)
On Error GoTo DirectoryError
    
    Dim sPathPart As String
    Dim i As Integer
    
    If Right(sPath, 1) <> "\" Then sPath = sPath & "\"
    
    For i = 1 To Len(sPath)
        If Mid(sPath, i, 1) = "\" Then
            sPathPart = Left(sPath, i - 1)
            MkDir sPathPart
        End If
    Next i
    
DirectoryError:
    Resume Next
    
End Sub
 
Hey wadda y know.

It works for UNC paths :-)

for some reason (and if i'm gonna assume anything it should be in the negative) i assumed that it wouldnt work with UNCs :cool:
 

Users who are viewing this thread

Back
Top Bottom